CS8383 OBJECT ORIENTED PROGRAMMING LABORATORY
List of Experiments
1 1. Electricity Bill Generation – Classes and Objects
2. Employee salary calculation – inheritance
3. Shape program - Abstract class
4. Stack Implementation – Interface
5. Currency converter
6. Array list operations
7. User defined Exception handling
8. File Manipulation
9. Multithreaded program
10. Calculator using event driven programming
11. Generic function
12. Mini project
a.
Banking Application [Deposit, Withdraw, Balance
Check, Amount Transfer, User profile] : using JDBC or Files
b.
Library Management [Book search, Loan
information, Reserve Book, Issue book, Return book] : using JDBC or Files
c.
Employee Management [ Add/ Modify/ Delete /
Search Employee information] : : using JDBC or Files
Observation and Record Format
Expt.no : 1 Electricity
Bill Generation – Classes and Objects
Date:
Objective:
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
Procedure
1. Design a base class ‘Connection’
with variables ‘consumerNumber’, ‘consumerName’, ‘previousMonthUnitsConsumed’,
‘currentMonthUnitsConsumed’ and ‘typeofEBConnection’ with the only method
‘calculateBill()’ which has no implementation.
2. Design two types of ‘Connection’ classes namely ‘DomesticConnection’ and ‘CommercialConnection’ which inherit from
the abstract base class (or interface) ‘Connection’.
3. Implement the method ‘calculateBill()’ for domestic connection class
as follows,
a.
First 100 units - Rs. 1 per unit,
b.
101-200 units - Rs. 2.50 per unit
c.
201 -500 units - Rs. 4 per unit
d.
501 units - Rs. 6 per unit.
4. Implement the method ‘calculateBill()’ for commercial connection class
as follows,
a.
First 100 units - Rs. 2 per unit,
b.
101-200 units - Rs. 4.50 per unit
c.
201 -500 units - Rs. 6 per unit
d.
501 units - Rs. 7 per unit.
5. Design a client class with main method to calculate electricity bill
based on the type of connection.
Program
Connection.java
public abstract class Connection{
protected int
consumerNumber;
protected String
consumerName;
protected int
previousMonthReading;
protected int
currentMonthReading;
protected String
typeofConnection;
protected double
billAmount;
public void
calculateBill();
}
DomesticConnection.java
public class DomesticConnection extends Connection {
public DomesticConnection(int cNumber,String
cName,int pmReading,int cmReading,String tConnection) {
consumerNumber=cNumber;
consumerName=cName;
previousMonthReading=pmReading;
currentMonthReading=cmReading;
typeofConnection=tConnection;
billAmount=0.0;
}
public void
calculateBill(){
if(currentMonthReading
<= 100)
billAmount
= currentMonthReading * 1;
else
if(currentMonthReading > 100 && currentMonthReading <= 200)
billAmount
= currentMonthReading * 2.5;
else
if(currentMonthReading > 200 && currentMonthReading <= 500)
billAmount
= currentMonthReading * 4;
else
if(currentMonthReading > 500)
billAmount
= currentMonthReading * 6;
}
public void
displayBill() {
System.out.println(“Consumer
number: ” + consumerNumber);
System.out.println(“Consumer
name: ” + consumerName);
System.out.println(“Units
consumed: ” + currentMonthReading);
System.out.println(“Bill
amount: ” + billAmount);
}
}
CommercialConnection.java
public class CommercialConnection extends
Connection {
public CommercialConnection(int
cNumber,String cName,int pmReading,int cmReading,String tConnection) {
…
}
public void calculateBill(){
…
}
public void displayBill() {
…
}
}
Client.java
import java.io.Scanner;
public class Client {
public
static void main(String args[]){
Connection
con = null;
String
conType=null;
int
menuOption = 0;
Scanner
in = null;
while(true)
{
System.out.println(“\t\t
Electricity Bill Application”);
System.out.println(“Enter the type of
connection: domestic or commercial”);
conType = in.nextString();
System.out.println(“1.
Calculate bill”);
System.out.println(“2.
Display bill”);
System.out.println(“3. Exit”);
System.out.println(“Select from
menu…”);
in
= new Scanner(System.in);
menuOption
= in.nextInt();
switch(menuOption) {
case 1:
if(conType.equals(”commercial”))
con = new
DomesticConnection();
else
con
= new CommercialConnection();
con.
calculateBill();
break;
case 2:
con.displayBill();
break;
case 3:
exit
1;
default:
System.out.println(“Enter
the correct option!”);
}
}
}
}
Output
Result