You are on page 1of 16

COMSATS University Islamabad, Lahore Campus

Department of Computer Science

Assignment 3 – Semester Fall 2022


Course Title: Object Oriented Programming Course Code: CSC241 Credit Hours: 4(3,1)
Course Instructor/s: Mr. Imran latif Program Name: BCE
Semester: 3rd Section: A, B Batch FA21-BCE
Total Marks: 10 Obtained Marks: 60 Minutes
Date:
Student’s Name: Abdullah Tariq Reg. No. FA20-BCE-029
Important Instruction:

 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 09, 2022 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)

public abstract class Employee


{
private String firstName;
private String lastName;
private String socialSecurityNumber;

private Date date;

// set first name


public void setFirstName( String
first )
{
firstName = first; // should
validate
} // end method setFirstName

// return first name


public String getFirstName()
{
return firstName;
} // end method getFirstName

// set last name


public void setLastName( String last )
{
lastName = last; // should validate
} // end method setLastName

// return last name


public String getLastName()
{
return lastName;
} // end method getLastName

// set social security number


public void
setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; //
should validate
} // end method setSocialSecurityNumber

// return social security number


public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber

// return String representation of


Employee object
public Date getDate() {
return date;
}

public void setDate(Date date) {


this.date = date;
}

public Employee(String firstName, String


lastName, String socialSecurityNumber, Date date) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber =
socialSecurityNumber;
this.date = date;
}

@Override
public String toString() {
return "Employee{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", socialSecurityNumber='" +
socialSecurityNumber + '\'' +
", date=" + date +
'}';
}

public abstract double earnings(); // no


implementation here
} // end abstract class Employee

public class BasePlusCommissionEmployee extends


CommissionEmployee
{
private double baseSalary; // base
salary per week
// six-argument constructor
public
BasePlusCommissionEmployee( String first, String
last,
String ssn,Date date, double sales,
double rate, double salary )
{
super( first, last, ssn,
date,sales, rate );
setBaseSalary( salary ); //
validate and store base salary
} // end six-argument
BasePlusCommissionEmployee constructor

// set base salary


public void setBaseSalary( double
salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw new
IllegalArgumentException(
"Base salary must be >= 0.0"
);
} // end method setBaseSalary

// return base salary


public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary

// calculate earnings; override method


earnings in CommissionEmployee
@Override
public double earnings()
{
return getBaseSalary() +
super.earnings();
}// end method earnings

// return String representation of


BasePlusCommissionEmployee object
@Override
public String toString()
{
return String.format( "%s %s; %s: $
%,.2f",
"base-salaried",
super.toString(),
"base salary",
getBaseSalary() );
} // end method toString
} // end class BasePlusCommissionEmployee

public class PayrollSystemTest


{
public static void main( String[]
args ) {
// create subclass objects
Date date1= new
Date(04,"july",2000);
SalariedEmployee salariedEmployee =
new SalariedEmployee("John", "Smith", "111-11-1111",
date1, 800.00);

Date date2= new


Date(05,"december",2001);
HourlyEmployee hourlyEmployee = new
HourlyEmployee("Karen", "Price", "222-22-2222",
date2,16.75, 40);
Date date3= new
Date(06,"august",2002);
CommissionEmployee
commissionEmployee = new CommissionEmployee(
"Sue", "Jones", "333-33-
3333",date3 ,10000, .06);

Date date4= new


Date(07,"january",2003);
BasePlusCommissionEmployee
basePlusCommissionEmployee = new
BasePlusCommissionEmployee(
"Bob", "Lewis",
"444-44-4444", date4,5000, .04, 300);

Employee[] emp = new Employee[5];


emp[0]= salariedEmployee;
emp[1]=basePlusCommissionEmployee;
emp[2]=hourlyEmployee;
emp[3]=commissionEmployee;
for(Employee emp1 : emp)
System.out.println(emp1);

}}

public class Date {


int day;
String month;
int year;

public int getDay() {


return day;
}

public void setDay(int day) {


this.day = day;
}
public String getMonth() {
return month;
}

public void setMonth(String month) {


this.month = month;
}

public int getYear() {


return year;
}

public void setYear(int year) {


this.year = year;
}

public Date(int day, String month, int year) {


this.day = day;
this.month = month;
this.year = year;
}

@Override
public String toString() {
return "Date{" +
"day=" + day +
", month='" + month + '\'' +
", year=" + year +
'}';
}
}

public class SalariedEmployee extends Employee


{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first,
String last, String ssn,Date date,
double salary )
{
super( first, last, ssn,date ); //
pass to Employee constructor
setWeeklySalary( salary ); //
validate and store salary
} // end four-argument
SalariedEmployee constructor

// set salary
public void setWeeklySalary( double
salary )
{
double baseSalary;
if ( salary >= 0.0 )
baseSalary = salary;
else
throw new
IllegalArgumentException(
"Weekly salary must be >=
0.0" );
} // end method setWeeklySalary

// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary

// calculate earnings; override


abstract method earnings in Employee
@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of
SalariedEmployee object
@Override
public String toString()
{

return String.format( "salaried


employee: %s\n%s: $%,.2f",
super.toString(), "weekly salary" ,
getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee

public class HourlyEmployee extends Employee


{
private double wage; // wage per hour
private double hours; // hours worked for week

// five-argument constructor
public HourlyEmployee( String first, String
last, String ssn,Date date,
double hourlyWage, double
hoursWorked )
{
super( first, last, ssn,date );
setWage( hourlyWage ); // validate hourly
wage
setHours( hoursWorked ); // validate hours
worked
} // end five-argument HourlyEmployee
constructor

// set wage
public void setWage( double hourlyWage )
{
if ( hourlyWage >= 0.0 )
wage = hourlyWage;
else
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0" );
} // end method setWage

// return wage
public double getWage()
{
return wage;
} // end method getWage

// set hours worked


public void setHours( double hoursWorked )
{
if ( ( hoursWorked >= 0.0 ) && ( hoursWorked
<= 168.0 ) )
hours = hoursWorked;
else
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and
<= 168.0" );
} // end method setHours

// return hours worked


public double getHours()
{
return hours;
} // end method getHours

// calculate earnings; override abstract method


earnings in Employee
@Override
public double earnings()
{
if ( getHours() <= 40 ) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() -
40 ) * getWage() * 1.5 ;
} // end method earnings

// return String representation of


HourlyEmployee object
@Override
public String toString()
{
return String.format( "hourly employee: %s\n
%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage" ,
getWage(),
"hours worked", getHours() );
} // end method toString
} // end class HourlyEmployee

public class CommissionEmployee extends Employee


{
private double grossSales; // gross weekly sales
private double commissionRate; // commission
percentage

// five-argument constructor
public CommissionEmployee( String first, String
last, String ssn,Date date,
double sales, double
rate )
{
super( first, last, ssn,date );
setGrossSales( sales );
setCommissionRate( rate );
} // end five-argument CommissionEmployee
constructor

// set commission rate


public void setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw new IllegalArgumentException(
"Commission rate must be > 0.0
and < 1.0" );
} // end method setCommissionRate

// return commission rate


public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate

// set gross sales amount


public void setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw new IllegalArgumentException(
"Gross sales must be >= 0.0" );
} // end method setGrossSales

// return gross sales amount


public double getGrossSales()
{
return grossSales;
} // end method getGrossSales

// calculate earnings; override abstract method


earnings in Employee
@Override
public double earnings()
{
return getCommissionRate() *
getGrossSales();
} // end method earnings

// return String representation of


CommissionEmployee object
@Override
public String toString()
{
return String.format( "%s: %s\n%s: $%,.2f;
%s: %.2f",
"commission employee",
super.toString(),
"gross sales", getGrossSales(),
"commission rate",
getCommissionRate() );
} // end method toString
} // end class CommissionEmployee

output
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

You might also like