Assignment - 3 Statement
Assignment - 3 Statement
Assignment - 3 Statement
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)
@Override
public String toString() {
return "Employee{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", socialSecurityNumber='" +
socialSecurityNumber + '\'' +
", date=" + date +
'}';
}
}}
@Override
public String toString() {
return "Date{" +
"day=" + day +
", month='" + month + '\'' +
", year=" + year +
'}';
}
}
// 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
// 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
// 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
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