DATE : 21/05.
2021 Object Oriented Programming
[Polymorphism in Java]
Lab# 08
Task 1: Create a payroll system using classes, inheritance and polymorphism
Four types of employees paid weekly
a. Salaried employees: fixed salary irrespective of hours
b. Hourly employees: 40 hours salary and overtime (> 40 hours)
c. Commission employees: paid by a percentage of sales
d. Base-plus-commission employees: base salary and a percentage of sales
The information know about each employee is his/her first name, last name and national
identity card number. The reset depends on the type of employee.
Step by Step Guidelines
Step 1: Define Employee Class
Being the base class, Employee class contains the common behavior. Add firstName,
lastName and CNIC as attributes of type String
Provide getter & setters for each attribute
Write default & parameterized constructors
Override toString() method as shown below
public String toString( ) {
return firstName + “ ” + lastName + “ CNIC# ” + CNIC ;
}
Define earning() method as shown below
public double earnings( ) {
return 0.00;
}
DUA E SAMEEN (02-131202-060) 1
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
Step 2: Define SalariedEmployee Class
Extend this class from Employee class.
Add weeklySalary as an attribute of type double
Provide getter & setters for this attribute. Make sure that weeklySalary never sets to
negative value. (use if)
Write default & parameterize constructor. Don’t forget to call default & parameterize
constructors of Employee class.
Override toString() method as shown below
public String toString( ) {
return “\nSalaried employee: ” + [Link]();
}
Override earning() method to implement class specific behavior as shown below
public double earnings( ) {
return weeklySalary;
}
Step 3: Define HourlyEmployee Class
Extend this class from Employee class.
Add wage and hours as attributes of type double
Provide getter & setters for these attributes. Make sure that wage and hours never set
to a negative value.
Write default & parameterize constructor. Don’t forget to call default &
parameterize constructors of Employee class.
Override toString() method as shown below
public String toString( ) {
DUA E SAMEEN (02-131202-060) 2
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
return “\nHourly employee: ” + [Link]();
}
Override earning() method to implement class specific behaviour as shown below
public double earnings( ) {
if (hours <= 40){
return wage * hours;
}
else{
return 40*wage + (hours-40)*wage*1.5;
}
}
Step 4: Define CommissionEmployee Class
Extend this class form Employee class.
Add grossSales and commissionRate as attributes of type double
Provide getter & setters for these attributes. Make sure that grossSales and
commissionRate never set to a negative value.
Write default & parameterize constructor. Don’t forget to call default &
parameterize constructors of Employee class.
Override toString() method as shown below
public String toString( ) {
return “\nCommission employee: ” + [Link]();
}
Override earning() method to implement class specific behaviour as shown below
public double earnings( ) {
return grossSales * commisionRate;
}
DUA E SAMEEN (02-131202-060) 3
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
Step 5: Define BasePlusCommissionEmployee Class
Extend this class form CommissionEmployee class not from Employee class. Why?
Think on it by yourself
Add baseSalary as an attribute of type double
Provide getter & setters for these attributes. Make sure that baseSalary never sets to
negative value.
Write default & parameterize constructor. Don’t forget to call default &
parameterize constructors of Employee class.
Override toString() method as shown below
public String toString( ) {
return “\nBase plus Commission employee: ” + [Link]();
}
Override earning() method to implement class specific behaviour as shown below
public double earnings( ) {
return baseSalary + [Link]();
}
Step 6: Putting it all Together
public class PayRollSystemTest {
public static void main (String [] args) {
Employee firstEmployee = new SalariedEmployee("Usman" ,"Ali","111-11-1111",
800.00 );
Employee secondEmployee = new CommissionEmployee("Atif" ,"Aslam", "222-22-
2222", 10000, 0.06 );
DUA E SAMEEN (02-131202-060) 4
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
Employee thirdEmployee = new BasePlusCommissionEmployee("Rana", "Naseeb",
"333-33-3333", 5000 , 0.04 , 300 );
Employee fourthEmployee = new HourlyEmployee( "Renson" , "Isaac", "444-44-4444" ,
16.75 , 40 );
// polymorphism: calling toString() and earning() on Employee’s reference
[Link](firstEmployee);
[Link]([Link]());
[Link](secondEmployee);
[Link]([Link]());
[Link](thirdEmployee);
// performing downcasting to access & raise base salary
BasePlusCommissionEmployee currentEmployee =
(BasePlusCommissionEmployee) thirdEmployee;
double oldBaseSalary = [Link]();
[Link]( "old base salary: " + oldBaseSalary) ;
[Link](1.10 * oldBaseSalary);
[Link]("new base salary with 10% increase is:"+
[Link]());
[Link]([Link]() );
[Link](fourthEmployee);
[Link]([Link]() );
DUA E SAMEEN (02-131202-060) 5
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
} // end main
} // end class
Solution:
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class Lab8Task1 {
public static void main(String[] args) {
Employee firstEmployee = new SalariedEmployee("Usman", "Hamza", "116-11-1911", 500.00);
Employee secondEmployee = new CommissionEmployee("Farhan", "Alam", "222-22-9222", 1000,
0.06);
Employee thirdEmployee = new BasePlusComissionEmployee("Sana", "Bhukhari", "333-34-3333",
5000, 0.04, 300);
Employee fourthEmployee = new HourlyEmployee("Nouman", "Ali", "444-44-4949", 123.67, 40);
[Link](firstEmployee);
[Link]([Link]());
[Link](secondEmployee);
[Link]([Link]());
[Link](thirdEmployee);
BasePlusComissionEmployee currentEmployee = (BasePlusComissionEmployee) thirdEmployee;
double oldBaseSalary = [Link]();
[Link]("old base salary: " + oldBaseSalary);
[Link](1.10 * oldBaseSalary);
[Link]("new base salary with 10% increase is:" + [Link]());
DUA E SAMEEN (02-131202-060) 6
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
[Link]([Link]());
[Link](fourthEmployee);
[Link]([Link]());
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class Employee {
private String FirstName;
private String LastName;
private String CNIC;
public String getFN() {
return FirstName;
}
public void setFN(String FirstName) {
[Link] = FirstName;
}
public String getLN() {
return LastName;
}
DUA E SAMEEN (02-131202-060) 7
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
public void setLN(String LastName) {
[Link] = LastName;
}
public String getCNIC() {
return CNIC;
}
public void setCNIC(String CNIC) {
[Link] = CNIC;
}
public Employee() {
}
public Employee(String first_name, String last_name, String cnic) {
FirstName = first_name;
LastName = last_name;
CNIC = cnic;
}
@Override
public String toString() {
return FirstName + " " + LastName + " CNIC# " + CNIC;
}
public double earnings() {
return 0.00;
}
DUA E SAMEEN (02-131202-060) 8
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class SalariedEmployee extends Employee {
private double weeklySalary;
public double getWS() {
return weeklySalary;
}
public void setWS(double weeklySalary) {
if (weeklySalary > 0) {
[Link] = weeklySalary;
} else {
[Link]("Enter positive value!");
}
}
public SalariedEmployee() {
public SalariedEmployee(String first_name, String last_name, String cnic, double salary) {
super(first_name, last_name, cnic);
[Link] = salary;
DUA E SAMEEN (02-131202-060) 9
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
@Override
public String toString() {
return "\nSalaried employee: " + [Link]();
}
@Override
public double earnings() {
return weeklySalary;
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class HourlyEmployee extends Employee {
private double wage, hours;
public double getwage() {
return wage;
}
public void setwage(String FirstName) {
[Link] = wage;
}
DUA E SAMEEN (02-131202-060) 10
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
public double gethours() {
return hours;
}
public void sethours(String FirstName) {
[Link] = hours;
}
public HourlyEmployee() {
public HourlyEmployee(String first_name, String last_name, String cnic, double wages, double hour) {
super(first_name, last_name, cnic);
[Link] = wages;
[Link] = hour;
}
@Override
public String toString() {
return "\nHourly employee: " + [Link]();
}
@Override
public double earnings() {
if (hours <= 40) {
return wage * hours;
} else {
return 40 * wage + (hours - 40) * wage * 1.5;
DUA E SAMEEN (02-131202-060) 11
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
}
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class CommissionEmployee extends Employee {
private double grossSales, commissionRate;
public double getGS() {
return grossSales;
}
public void setGS(double grossSales) {
if (grossSales > 0) {
[Link] = grossSales;
} else {
[Link]("Enter positive value!");
}
}
public double getCR() {
return commissionRate;
}
DUA E SAMEEN (02-131202-060) 12
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
public void setCR(double commissionRate) {
if (commissionRate > 0) {
[Link] = commissionRate;
} else {
[Link]("Enter positive value!");
}
}
public CommissionEmployee() {
public CommissionEmployee(String first_name, String last_name, String cnic, double grossSale,
double commissionrate) {
super(first_name, last_name, cnic);
[Link] = grossSale;
[Link] = commissionrate;
}
@Override
public String toString() {
return "\nCommission employee: " + [Link]();
}
@Override
public double earnings() {
return grossSales * commissionRate;
}
DUA E SAMEEN (02-131202-060) 13
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class BasePlusComissionEmployee extends Employee {
private double baseSalary;
public double getBS() {
return baseSalary;
}
public void setBS(double baseSalary) {
if (baseSalary > 0) {
[Link] = baseSalary;
} else {
[Link]("Enter positive value!");
}
}
public BasePlusComissionEmployee() {
public BasePlusComissionEmployee(String first_name, String last_name, String cnic, double
grossSale, double commissionrate, double basesalary) {
//super(first_name, last_name, cnic, grossSale, commissionrate);
[Link] = basesalary;
DUA E SAMEEN (02-131202-060) 14
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
@Override
public String toString() {
return "\nBase plus Commission employee: " + [Link]();
}
public double earnings() {
return baseSalary + [Link]();
}
}
Output:
Task 2: You have to implement the following diagram including some attributes and other
functions:
Solution: (Task already done in lab 7)
Output:
DUA E SAMEEN (02-131202-060) 15
DATE : 21/05.2021 Object Oriented Programming
[Polymorphism in Java]
DUA E SAMEEN (02-131202-060) 16