LAB1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

20S3L3 OBJECT ORIENTED PROGRAMMING LABORATORY LTPC

0 04 2
OBJECTIVES
To build software development skills using java programming for real-world
applications.
To understand and apply the concepts of classes, packages, interfaces, arraylist, exception
handling and file processing.
To develop applications using generic programming and event handling.

LIST OF EXPERIMENTS
1. Develop a Java application to generate Electricity bill. Create a class with the following
members: Consumer no., consumer name, previous month reading, current month reading, type
of EB connection (i.e domestic or commercial). Compute the bill amount using the following
tariff.
If the type of the EB connection is domestic, calculate the amount to be paid as follows:
First 100 units - Rs. 1 per unit
101-200 units - Rs. 2.50 per unit
201 -500 units - Rs. 4 per unit
> 501 units - Rs. 6 per unit
If the type of the EB connection is commercial, calculate the amount to be paid as follows:
First 100 units - Rs. 2 per unit
101-200 units - Rs. 4.50 per unit
201 -500 units - Rs. 6 per unit
> 501 units - Rs. 7 per unit

2. Develop a java application to implement currency converter (Dollar to INR, EURO to INR,
Yen to INR and vice versa), distance converter (meter to KM, miles to KM and vice versa), time
converter (hours to minutes, seconds and vice versa) using packages.

3. Develop a java application with Employee class with Emp_name, Emp_id, Address, Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate
Professor and Professor from employee class. Add Basic Pay (BP) as the member of all the
inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for
staff club fund. Generate pay slips for the employees with their gross and net salary.

4. Design a Java interface for ADT Stack. Implement this interface using array. Provide
necessary exception handling in both the implementations.
5. Write a program to perform string operations using ArrayList. Write functions for the
following
a. Append - add at end
b. Insert - add at particular index
c. Search
d. List all string starts with given letter

6. Write a Java Program to create an abstract class named Shape that contains two integers and
an empty method named print Area(). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape. Each one of the classes contains
only the method print Area () that prints the area of the given shape.

7. Write a Java program to implement user defined exception handling.

8. Write a Java program that reads a file name from the user, displays information about whether
the file exists, whether the file is readable, or writable, the type of file and the length of the file in
bytes.

9. Write a java program that implements a multi-threaded application that has three threads. First
thread generates a random integer every 1 second and if the value is even, second thread
computes the square of the number and prints. If the value is odd, the third thread will print the
value of cube of the number.

10. Write a java program to find the maximum value from the given type of elements using a
generic function.

11. Design a calculator using event-driven programming paradigm of Java with the following
options.
a) Decimal manipulations
b) Scientific manipulations

12. Develop a mini project for any application using Java concepts.

TOTAL : 60 PERIODS
OUTCOMES
Upon completion of the course, the students will be able to
Develop and implement Java programs for simple applications that make use of classes,
packages and interfaces.
Develop and implement Java programs with arraylist, exception handling and
multithreading.
Design applications using file processing, generic programming and event handling.
GENERATING ELECTRICITY BILL

AIM:

To Develop a Java application to generate Electricity bill.

ALGORITHM:

1. Import the java packages.


2. Create a class with members Consumer no., consumer name, previous month
reading, current month reading, type of EB connection (i.e domestic or commercial).
3. Class also contains methods domesticbillcalc and commercialbillcalc with its parameters
to compute bill amount.
4. Check whether the type of connection is domestic or
commercial. If domestic, calculate the bill amount as follows:
First 100 units - Rs. 1 per unit
101-200 units - Rs. 2.50 per unit
201 -500 units - Rs. 4 per unit
> 501 units - Rs. 6 per unit
If commercial, calculate the bill amount as follows:
First 100 units - Rs. 2 per unit
101-200 units - Rs. 4.50 per unit
201 -500 units - Rs. 6 per unit
> 501 units - Rs. 7 per unit
5. Calculate the units consumed by finding the differences between previous month
reading and current month reading.
6. By using Scanner class get the input during runtime.
7. Create object for a class in memory and assign it to the reference variable, then the
method is invoked.
8. Finally, the bill amount is displayed based on type of connection.

PROGRAM:

//File Name should be Saved as Ebbill.java

import java.io.*;
import java.util.*;
class ElectricityBill
{
double bill;
double domesticbillcalc (int units)
{
if(units<100)
bill = units * 1 ;
else if(units <= 200)
bill = 100 * 1 + (units - 100) * 2.50 ;
else if(units <= 500)
bill = 100 * 1 + 200 * 2.50 + (units - 200) * 4 ;
else
bill = 100 * 1 + 200 * 2.50 + 500 * 4 + (units - 500) * 6 ;
return bill;
}
double commercialbillcalc (int units)
{
if(units<100)
bill = units * 2 ;
else if(units <= 200)
bill = 100 * 1 + (units - 100) * 4.50 ;
else if(units <= 500)
bill = 100 * 1 + 200 * 4.50 + (units - 200) * 6 ;
else
bill = 100 * 1 + 200 * 4.50 + 500 * 6 + (units - 500) * 7 ;
return bill;
}
void show(String ptype,String consno,String consname,int pmr,int cmr,int units)
{
System.out.println("Type of Connection : " + ptype);
System.out.println("Consumer Number : " + consno);
System.out.println("Customer Name : " + consname);
System.out.println("Previous Month Reading : " + pmr);
System.out.println("Current Month Reading : " + cmr);
System.out.println("Units Consumed : " + units);
}
}

class Ebbill
{
public static void main(String[] args)
{
Scanner c = new Scanner(System.in);
System.out.println("Enter the Type of Connection :");
String ptype=c.next();
System.out.println("Enter the Consumer Number :");
String consno=c.next();
System.out.println("Enter the Consumer Name :");
String consname=c.next();
System.out.println("Enter the Previous Month Reading :");
int pmr=c.nextInt();
System.out.println("Enter the Current Month Reading :");
int cmr=c.nextInt();
int units = cmr-pmr;
ElectricityBill b = new ElectricityBill();
if(ptype.equalsIgnoreCase("DOMESTIC"))
{
b.show(ptype,consno,consname,pmr,cmr,units);
b.domesticbillcalc(units);
System.out.println("Bill to pay : " + b.bill);
}
else if(ptype.equalsIgnoreCase("COMMERCIAL"))
{
b.show(ptype,consno,consname,pmr,cmr,units);
b.commercialbillcalc(units);
System.out.println("Bill to pay : " + b.bill);
}
}
}

NOTE:
To Compile,
javac Ebbill.java
To Run
java Ebbill
OUTPUT:

RESULT:

Thus the application for generating Electricity bill has been successfully executed.
Viva questions:
1. How to calculate electricity bill?
2. How to calculate the Previous Month Reading?
3. How to calculate the Current Month Reading?
4. How to calculate the domesticbill?
5. How to calculate the commercialbill?
CURRENCY CONVERTER, DISTANCE CONVERTER AND TIME CONVERTER
USING PACKAGES

AIM:

To develop a java application to implement currency converter, distance converter


and time converter using packages.

ALGORITHM:

1. The package keyword is used to create a package in java.


2. Create a class CurrencyConverter inside a package name CurrencyConverter.
3. Class also contains methods dollortoinr, inrtodollor, eurotoinr, inrtoeuro, yentoinr, and
inrtoyen with its parameters to convert given currency.
4. Create a class DistanceConverter inside a package name DistanceConverter.
5. Class also contains methods metertokm, kmtometer, milestokm and kmtomiles with its
parameters to convert given distance.
6. Create a class TimeConverter inside a package name TimeConverter.
7. Class also contains methods hourstominutes, minutestohours, hourstoseconds and
secondstohours with its parameters to convert given time.
8. Import the CurrencyConverter, DistanceConverter, TimeConverter and other
java packages.
9. Create a class Converter and object for a class in memory and assign it to the reference
variable, then the method is invoked.
10. By using Scanner class get the choices for switch statement during runtime.
11. By using switch case statement we can convert currency, distance and time for each
choice.
12. Create object for a class in memory and assign it to the reference variable, then
the method is invoked.
13. Finally, the conversion is displayed based on type of converter.

PROGRAM:

//For Packages, Folder Name should be CurrencyConverter


//File Name should be CurrencyConverter.java

package CurrencyConverter;
public class CurrencyConverter
{
public double dollortoinr(double x)
{
double inr=x*67.86;
return inr;
}
public double inrtodollor(double x)
{
double dollor=x/67.86;
return dollor;
}
public double eurotoinr(double x)
{
double inr=x*79.18;
return inr;
}
public double inrtoeuro(double x)
{
double euro=x/79.18;
return euro;
}
public double yentoinr(double x)
{
double inr=x*0.62;
return inr;
}
public double inrtoyen(double x)
{
double yen=x/0.62;
return yen;
}
}

//For Packages, Folder Name should be DistanceConverter


//File Name should be DistanceConverter.java

package DistanceConverter;
public class DistanceConverter
{
public double metertokm(double x)
{
double km=x*0.001;
return km;
}
public double kmtometer(double x)
{
double meter=x/0.001;
return meter;
}
public double milestokm(double x)
{
double km=x*1.60934;
return km;
}
public double kmtomiles(double x)
{
double miles=x/1.60394;
return miles;
}
}

//For Packages, Folder Name should be TimeConverter


//File Name should be TimeConverter.java

package TimeConverter;
public class TimeConverter
{
public double hourstominutes(double x)
{
double minutes=x*60;
return minutes;
}
public double minutestohours(double x)
{
double hours=x/60;
return hours;
}
public double hourstoseconds(double x)
{
double seconds=x*3600;
return seconds;
}
public double secondstohours(double x)
{
double hours=x/3600;
return hours;
}
}

//File Name should be Converter.java separate this file from above 3 folders

import CurrencyConverter.*;
import DistanceConverter.*;
import TimeConverter.*;
import java.io.*;
import java.util.*;
class Converter
{
public static void main(String args[])
{
System.out.println("1.CurrencyConverter");
System.out.println("2.DistanceConverter");
System.out.println("3.TimeConverter");
Converter cr = new Converter();
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
String op = null;
switch(choice)
{
case 1: cr.Currency(); break;
case 2: cr.Distance(); break;
case 3: cr.Time(); break;
default:
System.out.println("Invalid case");
return;
}
}

public void Currency()


{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Currency Converter");
System.out.println("Enter the amount :");
double amt = in.nextInt();
CurrencyConverter cc = new CurrencyConverter();
System.out.println("DOLLOR="+amt+" is INR="+cc.dollortoinr(amt));
System.out.println("INR="+amt+" is DOLLOR="+cc.inrtodollor(amt));
System.out.println("EURO="+amt+" is INR="+cc.eurotoinr(amt));
System.out.println("INR="+amt+" is EURO="+cc.inrtoeuro(amt));
System.out.println("YEN="+amt+" is INR="+cc.yentoinr(amt));
System.out.println("INR="+amt+" is YEN="+cc.inrtoyen(amt));
}

public void Distance()


{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Distance Converter");
System.out.println("Enter the distance :");
double dis = in.nextInt();
DistanceConverter dd = new DistanceConverter();
System.out.println("METER="+dis+" is KM="+dd.metertokm(dis));
System.out.println("KM="+dis+" is METER="+dd.kmtometer(dis));
System.out.println("MILES="+dis+" is KM="+dd.milestokm(dis));
System.out.println("KM="+dis+" is MILES="+dd.kmtomiles(dis));
}

public void Time()


{
Scanner out = new Scanner(System.in);
System.out.println("Welcome to Time Converter");
System.out.println("Enter the time :");
double tim = out.nextInt();
TimeConverter tt = new TimeConverter();
System.out.println("HOURS="+tim+" is MINUTES="+tt.
hourstominutes(tim)); System.out.println("MINUTES="+tim+" is
HOURS="+tt.minutestohours(tim)); System.out.println("HOURS="+tim+" is
SECONDS="+tt.hourstoseconds(tim));
System.out.println("SECONDS="+tim+" is HOURS="+tt.secondstohours(tim));
}
}

NOTE:

To Compile, go to CurrencyConverter folder


javac CurrencyConverter.java

To Compile, go to DistanceConverter folder


javac DistanceConverter.java

To Compile, go to TimeConverter folder


javac TimeConverter.java

To Compile,
javac Converter.java
To Run
java Converter
OUTPUT:

RESULT:

Thus the application for currency converter, distance converter and time converter
using packages has been successfully executed.
Viva questions:
1. How to create a package in java?
2. How to Create a class CurrencyConverter inside a package name CurrencyConverter?
3. How to Create a class DistanceConverter inside a package name DistanceConverter?
4. How to Create object for a class in memory?
5. How to Create a class TimeConverter inside a package name TimeConverter

You might also like