0% found this document useful (0 votes)
247 views

Lab 08 - Polymorphism

This document provides guidance on an object-oriented payroll system lab exercise in Java. It involves: 1. Creating an Employee class hierarchy with SalariedEmployee, HourlyEmployee, CommissionEmployee, and BasePlusCommissionEmployee subclasses. 2. Implementing polymorphism through overriding methods like toString() and earnings() in the subclasses. 3. Testing the classes by creating objects, calling overridden methods, and performing downcasting. 4. A second exercise involves implementing Shape, Circle, and Rectangle classes with polymorphism through overridden toString() methods. Testing involves a menu to add/remove shapes and display properties. 5. Students are tasked with finding other examples of polymorphism in their

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views

Lab 08 - Polymorphism

This document provides guidance on an object-oriented payroll system lab exercise in Java. It involves: 1. Creating an Employee class hierarchy with SalariedEmployee, HourlyEmployee, CommissionEmployee, and BasePlusCommissionEmployee subclasses. 2. Implementing polymorphism through overriding methods like toString() and earnings() in the subclasses. 3. Testing the classes by creating objects, calling overridden methods, and performing downcasting. 4. A second exercise involves implementing Shape, Circle, and Rectangle classes with polymorphism through overridden toString() methods. Testing involves a menu to add/remove shapes and display properties. 5. Students are tasked with finding other examples of polymorphism in their

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

CSL-210: Object Oriented

Programming
Semester BS CS – 02

Lab08: Polymorphism
In this lab, the following topics will be covered:

1. Polymorphism
2. upcasting and downcasting
3. instanceof operator
4. Exercise for practice

Exercises

Exercise 1 (a)

Create a payroll system using classes, inheritance and polymorphism


Four types of employees paid weekly
1. Salaried employees: fixed salary irrespective of hours
2. Hourly employees: 40 hours salary and overtime (> 40 hours)
3. Commission employees: paid by a percentage of sales
4. 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
Department of Computer Sciences Semester BSCS 2
CSL-210 Object Oriented Programming Lab 08: Polymorphism
 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;
}

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: ” + super.toString();
}

 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( ) {
return “\nHourly employee: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
if (hours <= 40){
return wage * hours;
}
else{

Department of Computer Sciences Semester BSCS 2


CSL-210 Object Oriented Programming Lab 08: Polymorphism
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: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
return grossSales * commisionRate;
}

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: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
return baseSalary + super.earning();
}

Exercise 1 (b)

Step 6: Putting it all Together

public class PayRollSystemTest {


public static void main (String [] args) {

Department of Computer Sciences Semester BSCS 2


CSL-210 Object Oriented Programming Lab 08: Polymorphism
Employee firstEmployee = new SalariedEmployee("Usman" ,"Ali","111-
11-1111", 800.00 );

Employee secondEmployee = new CommissionEmployee("Atif" ,"Aslam",


"222-22-2222", 10000, 0.06 );

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
System.out.println(firstEmployee);
System.out.println(firstEmployee.earnings());
System.out.println(secondEmployee);
System.out.println(secondEmployee.earnings());

System.out.println(thirdEmployee);
// performing downcasting to access & raise base salary
BasePlusCommissionEmployee currentEmployee =
(BasePlusCommissionEmployee) thirdEmployee;

double oldBaseSalary = currentEmployee.getBaseSalary();


System.out.println( "old base salary: " + oldBaseSalary) ;

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

Department of Computer Sciences Semester BSCS 2


CSL-210 Object Oriented Programming Lab 08: Polymorphism
Exercise 2 (a)

Implement classes: Shape, Circle and Rectangle based on the class diagram and description below:

Class Point implementation is given as follow:

class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x;}
public int getY() { return y;}
public double distanceTo(Point p) {
return Math.sqrt((x-p.getX())*(x-p.getX())+
(y-p.getY())*(y-p.getY()));
}
public String toString() {
return "("+x+", "+y+")";
}
}

Class Shape has:

 An attributes of type Point, specifies the center of the shape object.


 A constructor that allows to initialize the center attribute with the value of the passed parameter
 A method that takes an object of type Point as a parameter and returns true if the point resides within the
shape’s area, and false otherwise.

Class Circle has:

 An attribute of type integer specifies the radius measure of the circle

Department of Computer Sciences Semester BSCS 2


CSL-210 Object Oriented Programming Lab 08: Polymorphism
 A constructor that takes a Point parameter to initialize the center and an integer parameter to initialize the
radius
 A getRadius method to return the value of the attribute radius
 An overriding version of toString method to return the attribute values of a Circle object as String

Class Rectangle has:

 Two integer attributes represents the length and width of the Rectangle object
 A constructor to initialize the center, length and width attribute for a new Rectangle object
 Methods getLength and getWidth returns the values of attributes length and width respectively
 An overriding version of toString method to return the attribute values of a Rectangle object as a String

Class ShapesArray

 displayrectsinfo() display all rectangles information


 getCirclecounter():int return the number of circles
 getAvgAreas():double return the average area of all shapes
 removeallrect() delete all rectangles

Exercise 2 (b)

Step 6: Putting it all Together

Implementation TestShape as given.

create ShapesArray object with size=20


display these options

1. add new shape


a. for rectangle (ask for details)
b. for circle (ask for details)
2. display all rectangles
3. display the average shapes area
4. display the number of circles
5. remove all rectangles
6. exit

Project

 From last week you have found the Inheritance between the classes and now find the
Polymorphism in your classes.

Department of Computer Sciences Semester BSCS 2


CSL-210 Object Oriented Programming Lab 08: Polymorphism

You might also like