Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

COMSATS University Islamabad, Lahore Campus

Department of Computer Science

Assignment 3 – Semester Fall 2021


Course Title: Object Oriented Programming Course Code: CSC241 Credit Hours: 4(3,1)
Course Instructor/s: Mr. Imran latif Program Name: BEE
rd
Semester: 3 Section: B Batch FA20-BEE
Total Marks: 10 Obtained Marks: Date:
60 Minutes
Student’s Name: Obtain Reg. No.
Important Instruction:
ed
Marks:
• Student is himself/herself responsible for successful submission of assignment on CU-Online
• Your submission must include the following in a single pdf file.
1. Code of all classes
2. Snapshot of the output of submitted code.
• Copied assignment will get zero credit.
• Deadline: December 11, 2021 till 11:30 PM

Question 1:
Recall the concept of Inheritance, method overriding and polymorphism and write down the
code according to requirements.
Learning Outcome: PLO3→CLO3 (C3, C4, C5)
Question 1:

A company pays its employees on a weekly basis. The employees are of four types: Salaried
employees are paid a fixed weekly salary regardless of the number of hours worked, hourly
employees are paid by the hour and receive overtime pay for all hours worked in excess of 40
hours, commission employees are paid a percentage of their sales and salaried- commission
employees receive a base salary plus a percentage of their sales. For the current pay period,
the company has decided to reward salaried-commission employees by adding 10% to their
base salaries. The company wants to implement a Java application that performs its payroll
calculations polymorphically.
• Modify the above payroll system to include private instance variable birthDate in class
Employee. Add get methods to class Date. Assume that payroll is processed once per
month.
• Include an additional Employee subclass PieceWorker that represents an employee whose
pay is based on the number of pieces of merchandise produced. Class PieceWorker should
contain private instance variables wage (to store the employee’s wage per piece) and pieces
(to store the number of pieces produced). Provide a concrete implementation of method
earnings in class PieceWorker that calculates the employee’s earnings by multiplying the
number of pieces produced by the wage per piece. Create an array of Employee variables
to store references to the various employee objects. In a loop, for each Employee, display
its String representation and calculate the payroll for each Employee (polymorphically),
and add a $100.00 bonus to the person’s payroll amount if the current month is the one in
which the Employee’s birthday occurs or an employee object is of type
BasePlusCommissionEmployee

Answers:

Employee class:

public abstract class Employee {

private String firstName;

private String lastName;

private String socialSecurityNumber;

public Employee(String firstName, String lastName, String ssn) {

this.firstName = firstName;

this.lastName = lastName;

this.socialSecurityNumber = ssn;

public String getFirstName() {

return firstName;

}
public void setFirstName(String firstName) {

this.firstName = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getSocialSecurityNumber() {

return socialSecurityNumber;

public void setSocialSecurityNumber(String ssn) {

this.socialSecurityNumber = ssn;

@Override

public String toString(){

return String.format("%s %s ssn: %s", getFirstName(), getLastName(), getSocialSecurityNumber());

}
HourlyEmployee:

public class HourlyEmployee extends Employee{

private double wage; //wage per hour

private double hours;// hours worked for week

public HourlyEmployee(String firstName, String lastName, String ssn, double hours, double wage) {

super(firstName, lastName, ssn);

this.setHours(hours);

this.setWage(wage);

public double getWage() {

return wage;

public void setWage(double hourlyWage) {

if(hourlyWage >= 0.0){

this.wage = hourlyWage;

}else

throw new IllegalArgumentException("HourLY Wage must be >=0.0");

public double getHours() {


return hours;

public void setHours(double hourWorked) {

if(hourWorked >= 0.0){

this.hours = hourWorked;

}else

throw new IllegalArgumentException("Hour must be >=0.0 and <=168"); //work hour per week
7*24hrs

@Override

public double earnings() {

if(getHours() <= 40){

return getWage()*getHours();

}else{

return getWage()*getHours()+(getHours()-40)*getWage()*1.5;

@Override

public String toString()

return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", super.toString(), "hourly
wage" , getWage(), "hours worked", getHours() );

}
SalariedEmployee:

public class SalariedEmployee extends Employee{

private double weeklySalary;

public SalariedEmployee(String firstName, String lastName, String ssn, double salary) {

super(firstName, lastName, ssn);

this.setWeeklySalary(salary);

public double getWeeklySalary() {

return weeklySalary;

public void setWeeklySalary(double salary) {

if(salary >= 0.0){

this.weeklySalary = salary;

}else

throw new IllegalArgumentException("Weekly Salary Must be >= 0.0");

@Override

public double earnings() {

return getWeeklySalary();

}
@Override

public String toString(){

return String.format("salaried employee: %s\n%s: $%,.2f", super.toString(), "weekly salary" ,


getWeeklySalary());

CommissionEmployee

public class CommissionEmployee extends Employee{

private double grossSales;

private double commissionRate;

public CommissionEmployee(String firstName, String lastName, String ssn, double sales, double rate)
{

super(firstName, lastName, ssn);

this.setCommissionRate(rate);

this.setGrossSales(sales);

public double getGrossSales() {

return grossSales;

public void setGrossSales(double sales) {

if(sales >= 0.0){

this.grossSales = sales;

}else
throw new IllegalArgumentException("gross sales must be > 0.0");

public double getCommissionRate() {

return commissionRate;

public void setCommissionRate(double rate) {

if(rate > 0.0 && rate < 1.0){

this.commissionRate = rate;

}else

throw new IllegalArgumentException("Commission rate must be > 0 and < 1.0");

@Override

public double earnings() {

return getGrossSales()*getCommissionRate();

@Override

public String toString(){

return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f","commission employee", super.toString(),


"gross sales", getGrossSales(), "commission rate", getCommissionRate() );

BasePlusCommissionEmployee

public class BasePlusCommissionEmployee extends CommissionEmployee{


private double baseSalary;

public BasePlusCommissionEmployee(String firstName, String lastName, String ssn, double sales,


double rate, double salary) {

super(firstName, lastName, ssn, sales, rate);

this.setBaseSalary(salary);

public double getBaseSalary() {

return baseSalary;

public void setBaseSalary(double salary) {

if(salary >= 0.0){

this.baseSalary = salary;

}else

throw new IllegalArgumentException("Base salary must be >0.0");

// calculate earnings; override method earnings in CommissionEmployee

@Override

public double earnings() {

return getBaseSalary() + super.earnings();

@Override

public String toString()


{

return String.format( "%s %s; %s: $%,.2f",

"base-salaried", super.toString(),

"base salary", getBaseSalary() );

PieceWorker

public class PieceWorker extends Employee {

private double wage;

private double piece;

public PieceWorker(double wage, double piece, String firstName, String lastName, String
socialSecurityNumber) {

super(firstName, lastName, socialSecurityNumber);

if(wage<0.0)

throw new IllegalArgumentException("wage per piece must be greater or eqal to 0.0");

if(piece<0.0)

throw new IllegalArgumentException("pieces sold must be greater or eqal to 0.0");

this.wage = wage;

this.piece = piece;

public void setWage(double wage){

if(wage<0.0)

throw new IllegalArgumentException("wage per piece must be greater or eqal to 0.0");

this.wage = wage;
}

public double getWage() {

return wage;

public void setPiece(double piece) {

if(piece<0.0)

throw new IllegalArgumentException("pieces sold must be greater or eqal to 0.0");

this.piece = piece;

public double getPiece() {

return piece;

Okay }

@Override

public double earnings(){

return getWage()*getPiece();

@Override

public String toString() {

return String.format("%s %s; %s $%,.2f","Piece Worker", super.toString(), "Wage per piece",


getWage(), "Piece sold", getPiece());

}
}

Test Class:

*PayrollSystemTest*

public class PayrollSystemTest {

public static SalariedEmployee salariedEmployee;

public static HourlyEmployee hourlyEmployee;

public static ComissionEmployee commissionEmployee;

public static BasePlusComissionEmployee basePlusCommissionEmployee;

public static PieceWorker pieceworker;

public static void main(String[] args) {

Employee[] employees = new Employee[5];

employees[0] = new SalariedEmployee(“Ifrah", "Abid", "A001", 680000.00);

employees[1] = new HourlyEmployee("Ali", "Scheme", "A002", 17.00, 9000);

employees[2] = new ComissionEmployee("Tooba", "Gilani", "A003", 9100000, 0.04);

employees[3] = new BasePlusComissionEmployee("Wahaj", "Tidda", "A004", 2120000, .03,


590000);

employees[4] = new PieceWorker("Besto", "Firendo", "A004", 2120000, .03, 590000);

System.out.println("Employees processed polymorphically:\n");

for (Employee currentEmployee : employees) {

System.out.println(currentEmployee); // invokes toString


if (currentEmployee instanceof BasePlusComissionEmployee) {

BasePlusComissionEmployee employee = (BasePlusComissionEmployee) currentEmployee;

employee.setBaseSalary(1.10 * employee.getBaseSalary());

System.out.printf( "new base salary with 10%% increase is: $%,.2f\n",


employee.getBaseSalary());

System.out.printf( "earned $%,.2f\n\n", currentEmployee.earnings());

Question 2:

Shape

public abstract class Shape {

private String color; private boolean filled;

public Shape() {

public Shape(String color, boolean filled) { this.color = color; this.filled = filled;

public String getColor() { return color;

public void setColor(String color) { this.color = color;


}

public boolean isFilled() { return filled;

public void setFilled(boolean filled) { this.filled = filled;

public abstract double getArea();

public abstract double getPerimeter();

@Override

public String toString() { color + ", filled=" + filled + "}";

Circle:

return "Shape {color=" +

public class Circle extends Shape {

private double radius;

public Circle() {

public Circle(double radius) { this.radius = radius;

public Circle(double radius, String color, boolean filled) { super(color, filled); this.radius = radius;

public double getRadius() { return radius;

public void setRadius(double radius) { this.radius = radius;


}

@Override public double getArea() { return 3.14 * radius * radius;

@Override public double getPerimeter() { return 2 * 3.14 * radius;

@Override public String toString() { return super.toString() + "Circle {radius=" + radius + "}";

Rectangle:

public class Rectangle extends Shape {

private double width; private double length;

public Rectangle() {

public Rectangle(double width, double length) { this.width = width;

this.length = length; }

public Rectangle(double width, double length, String color, boolean filled) {

super(color, filled); this.width = width; this.length = length;

public double getWidth() { return width;

public void setWidth(double width) { this.width = width;

public double getLength() { return length;

public void setLength(double length) { this.length = length;


}

@Override public double getArea() { return length * width;

@Override public double getPerimeter() { return 2 * (length + width);

@Override

public String toString() { return super.toString() + "Rectangle {width=" + width + ", length="

+ length + "}"; }

Test:

import java.util.Scanner;

public class Test {

public static void main(String[] args) { Shape[] shapes = new Shape[4];

shapes[0] = new Circle(3.2, "Red", true); shapes[1] = new

Circle(2.5, "Blue", false); shapes[2] = new

Rectangle(2.0, 4.0, "Yellow", false); shapes[3] = new Rectangle(3.0, 5.0, "Green", true);

Scanner input = new Scanner(System.in);

System.out.print("Enter choice, 1 for Circle, 2 for Rectangle:"); int choice = input.nextInt(); if (choice ==
1) {

System.out.print("Enter radius:"); double radius = input.nextDouble();

for (int i = 0; i < shapes.length; i++) {

if (shapes[i] instanceof Circle) {

Circle circle = (Circle) shapes[i]; circle.setRadius(radius);

}}

} else if (choice == 2) { System.out.print("Enter length:");


double length = input.nextDouble(); System.out.print("Enter width:"); width = input.nextDouble();

double

for (int i = 0; i < shapes.length; i++) { if

(shapes[i] instanceof Rectangle) { rectangle = (Rectangle) shapes[i]; rectangle.setLength(length);


rectangle.setWidth(width);

}}

} shapesSummary(shapes); input.close();

Rectangle

public static void shapesSummary(Shape[] shapes) {

for (int i = 0; i < shapes.length; i++) { System.out.println(shapes[i]);

System.out.println("Area:" + shapes[i].getArea()); System.out.println("Perimeter:" +


shapes[i].getPerimeter());

Output:

You might also like