Lab 8
Lab 8
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
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 ;
}
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: ” + super.toString();
}
System.out.println(thirdEmployee);
// performing downcasting to access & raise base salary
BasePlusCommissionEmployee currentEmployee =
(BasePlusCommissionEmployee) thirdEmployee;
currentEmployee.setBaseSalary(1.10 * oldBaseSalary);
System.out.println("new base salary with 10% increase is:"+
currentEmployee.getBaseSalary());
System.out.println(thirdEmployee.earnings() );
System.out.println(fourthEmployee);
System.out.println(fourthEmployee.earnings() );
} // 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);
System.out.println(firstEmployee);
System.out.println(firstEmployee.earnings());
System.out.println(secondEmployee);
System.out.println(secondEmployee.earnings());
System.out.println(thirdEmployee);
BasePlusComissionEmployee currentEmployee = (BasePlusComissionEmployee) thirdEmployee;
double oldBaseSalary = currentEmployee.getBS();
System.out.println("old base salary: " + oldBaseSalary);
currentEmployee.setBS(1.10 * oldBaseSalary);
System.out.println("new base salary with 10% increase is:" + currentEmployee.getBS());
System.out.println(thirdEmployee.earnings());
System.out.println(fourthEmployee);
System.out.println(fourthEmployee.earnings());
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class Employee {
public Employee() {
}
@Override
public String toString() {
return FirstName + " " + LastName + " CNIC# " + CNIC;
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class SalariedEmployee extends Employee {
public SalariedEmployee() {
@Override
public String toString() {
return "\nSalaried employee: " + super.toString();
}
@Override
public double earnings() {
return weeklySalary;
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class HourlyEmployee extends Employee {
public HourlyEmployee() {
public HourlyEmployee(String first_name, String last_name, String cnic, double wages, double hour) {
super(first_name, last_name, cnic);
this.wage = wages;
this.hours = hour;
}
@Override
public String toString() {
return "\nHourly employee: " + super.toString();
}
@Override
public double earnings() {
if (hours <= 40) {
return wage * hours;
} else {
return 40 * wage + (hours - 40) * wage * 1.5;
}
}
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class CommissionEmployee extends Employee {
public CommissionEmployee() {
@Override
public String toString() {
return "\nCommission employee: " + super.toString();
}
@Override
public double earnings() {
return grossSales * commissionRate;
}
package lab8.task1;
/**
*
* @author Sameen Arshad
*/
public class BasePlusComissionEmployee extends Employee {
public BasePlusComissionEmployee() {
@Override
public String toString() {
return "\nBase plus Commission employee: " + super.toString();
}
Output:
Task 2: You have to implement the following diagram including some attributes and other
functions:
Solution: (Task already done in lab 7)
Output: