Fiza Oop 8
Fiza Oop 8
Karachi Campus
Submitted By:
Submitted To:
2 You have to implement the following diagram including some attributes and other
functions.
Submitted On:
2/5/2024
(Date: DD/MM/YY)
26/4/2024 Object Oriented Programming
[Polymorphism]
LAB # 08
Task # 01:
Create a payroll system using classes, inheritance and polymorphism
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.
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 ;
}
FIZA KHAN 1
26/4/2024 Object Oriented Programming
[Polymorphism]
FIZA KHAN 2
26/4/2024 Object Oriented Programming
[Polymorphism]
}
}
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();
}
FIZA KHAN 3
26/4/2024 Object Oriented Programming
[Polymorphism]
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
FIZA KHAN 4
26/4/2024 Object Oriented Programming
[Polymorphism]
Solution:
Employee class:
package task.pkg1;
public Employee() {
this.fn = "";
this.ln = "";
this.cnic = "";
}
@Override
public String toString() {
return fn + " " + ln + " CNIC# " +cnic ;
}
FIZA KHAN 5
26/4/2024 Object Oriented Programming
[Polymorphism]
SalariedEmployee class:
package task.pkg1;
public SalariedEmployee() {
super();
this.weeklySalary = 0.0;
}
@Override
public String toString() {
return "\nSalaried employee: "+ super.toString();
}
@Override
public double earnings() {
return weeklySalary;
}
HourlyEmployee class:
package task.pkg1;
FIZA KHAN 6
26/4/2024 Object Oriented Programming
[Polymorphism]
public HourlyEmployee() {
super();
this.wage = 0.0;
this.hours = 0.0;
}
@Override
public double earnings() {
if (hours <= 40){
return wage * hours;
}
else{
return 40*wage + (hours-40)*wage*1.5;
}
}
@Override
public String toString() {
return "\nHourly employee: " + super.toString();
}
CommissionEmployee class:
package task.pkg1;
FIZA KHAN 7
26/4/2024 Object Oriented Programming
[Polymorphism]
public CommissionEmployee() {
super();
this.grossSales = 0.0;
this.commissionRate = 0.0;
}
@Override
public double earnings() {
return grossSales * commissionRate;
}
@Override
public String toString() {
return "\nCommission employee: " + super.toString();
}
BasePlusCommissionEmployee class:
package task.pkg1;
FIZA KHAN 8
26/4/2024 Object Oriented Programming
[Polymorphism]
public BasePlusCommissionEmployee() {
super();
this.baseSalary = 0.0;
}
@Override
public String toString() {
return "\nBase plus Commission employee: " + super.toString();
}
@Override
public double earnings() {
return baseSalary + super.earnings();
}
Output:
FIZA KHAN 9
26/4/2024 Object Oriented Programming
[Polymorphism]
Task # 02:
You have to implement the following diagram including some attributes and other functions:
Solution:
Shape class:
package shape;
@Override
public String toString() {
return "Area: " + getArea();
FIZA KHAN 10
26/4/2024 Object Oriented Programming
[Polymorphism]
Square class:
package shape;
@Override
public double getArea() {
return Math.pow(length, 2);
}
@Override
public String toString() {
System.out.println("Length: "+length);
return super.toString();
}
Rectangle class:
package shape;
@Override
public double getArea() {
return getLength()*width;
}
@Override
public String toString() {
System.out.println("Width: "+width);
return super.toString();
}
FIZA KHAN 11
26/4/2024 Object Oriented Programming
[Polymorphism]
Circle class:
package shape;
@Override
public double getArea() {
return Math.PI*Math.pow(radius, 2);
}
@Override
public String toString() {
System.out.println("Radius: "+radius);
return super.toString();
}
Main class:
package shape;
FIZA KHAN 12
26/4/2024 Object Oriented Programming
[Polymorphism]
Output:
FIZA KHAN 13