Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

problem Modify the payroll system of Figs. 10.4 - 10.9 (available in the Chapter

ID: 3915955 • Letter: P

Question

problem

Modify the payroll system of Figs. 10.4 - 10.9 (available in the Chapter 10 slide on D2L). 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 variable 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 objects of each concrete class in the new Employee hierarchy. For each Employee, display its String representation and earnings.

provided code

// Employee hierarchy test program.
import java.util.Scanner; // program uses Scanner to obtain user input

public class PayrollSystemTest {
public static void main(String[] args) {
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee(
"John", "Smith", "111-11-1111", 6, 15, 1944, 800.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee(
"Karen", "Price", "222-22-2222", 12, 29, 1960, 16.75, 40);
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 9, 8, 1954, 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 3, 2, 1965, 5000, .04, 300);

System.out.println("Employees processed individually: ");
  
System.out.printf("%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings());

// create four-element Employee array
Employee[] employees = new Employee[4];

// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;

Scanner input = new Scanner(System.in); // to get current month
int currentMonth;

// get and validate current month
do {
System.out.print("Enter the current month (1 - 12): ");
currentMonth = input.nextInt();
System.out.println();
} while ((currentMonth < 1) || (currentMonth > 12));

System.out.println("Employees processed polymorphically: ");
  
// generically process each element in array employees
for (Employee currentEmployee : employees) {
System.out.println(currentEmployee); // invokes toString

// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee) {
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
(BasePlusCommissionEmployee) currentEmployee;

double oldBaseSalary = employee.getBaseSalary();
employee.setBaseSalary(1.10 * oldBaseSalary);
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary());
}

// if month of employee's birthday, add $100 to salary
if (currentMonth == currentEmployee.getBirthDate().getMonth()) {
System.out.printf(
"earned $%,.2f %s ", currentEmployee.earnings(),
"plus $100.00 birthday bonus");
}
else {
System.out.printf(
"earned $%,.2f ", currentEmployee.earnings());
}
}

// get type name of each object in employees array
for (int j = 0; j < employees.length; j++) {
System.out.printf("Employee %d is a %s ", j,
employees[j].getClass().getName());
}
}
}

Explanation / Answer

package cheggjuly;

public class PieceWorker extends Employee{

private double wage;

private int pieces;

public PieceWorker(double wage,int pieces){

this.wage=wage;

this.pieces=pieces;

}

public double earnings(){

return this.wage*this.pieces;

}

public double getWage() {

return wage;

}

public void setWage(double wage) {

this.wage = wage;

}

public int getPieces() {

return pieces;

}

public void setPieces(int pieces) {

this.pieces = pieces;

}

}

PayrollTester:

package cheggjuly;
import java.util.Scanner; // program uses Scanner to obtain user input

public class PayrollSystemTest {
public static void main(String[] args) {
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee(
"John", "Smith", "111-11-1111", 6, 15, 1944, 800.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee(
"Karen", "Price", "222-22-2222", 12, 29, 1960, 16.75, 40);
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 9, 8, 1954, 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 3, 2, 1965, 5000, .04, 300);
PieceWorker pieceWorker=new PieceWorker(10,20);
System.out.println("Employees processed individually: ");
  
System.out.printf("%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings());
System.out.printf("%s %s: $%,.2f ",
pieceWorker,
"earned", pieceWorker.earnings());

// create four-element Employee array
Employee[] employees = new Employee[5];

// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;
employees[4]=pieceWorker;
Scanner input = new Scanner(System.in); // to get current month
int currentMonth;

// get and validate current month
do {
System.out.print("Enter the current month (1 - 12): ");
currentMonth = input.nextInt();
System.out.println();
} while ((currentMonth < 1) || (currentMonth > 12));

System.out.println("Employees processed polymorphically: ");
  
// generically process each element in array employees
for (Employee currentEmployee : employees) {
System.out.println(currentEmployee); // invokes toString

// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee) {
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
(BasePlusCommissionEmployee) currentEmployee;

double oldBaseSalary = employee.getBaseSalary();
employee.setBaseSalary(1.10 * oldBaseSalary);
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary());
}

// if month of employee's birthday, add $100 to salary
if (currentMonth == currentEmployee.getBirthDate().getMonth()) {
System.out.printf(
"earned $%,.2f %s ", currentEmployee.earnings(),
"plus $100.00 birthday bonus");
}
else {
System.out.printf(
"earned $%,.2f ", currentEmployee.earnings());
}
}

// get type name of each object in employees array
for (int j = 0; j < employees.length; j++) {
System.out.printf("Employee %d is a %s ", j,
employees[j].getClass().getName());
}
}
}

modified tester code to add piece worker and display his earnings.

Expected output is not provided as there are no files for other subclasses please execute it on your side and let me know if face any issues.

Added the additional class for piece worker and method to calculate his earnings.