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

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

ID: 3911185 • Letter: M

Question

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());
}
}
}

new provided code please look at is as it has changed

********EMPLOYEE CLASS************

import java.util.Date;

public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthday;
public Employee( String first, String last, String ssn , Date DayOfBirth)
{
this.firstName = first;
this.lastName = last;
this.socialSecurityNumber = ssn;
this.birthday = DayOfBirth;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn;
}
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
public Date getBirthday()
{
return birthday;
}

public String toString()
{
return String.format( "%s %s social security number: %s birth date: %s ",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthday().toString());
}
public abstract double earnings();
}

*************SALARIEDCLASS*********

import java.util.Date;

public class SalariedEmployee extends Employee
{
private double weeklySalary;
public SalariedEmployee( String first, String last, String ssn, Date birthDate, double salary )
{
super( first, last, ssn, birthDate);
setWeeklySalary( salary );
}

  
public final void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public double earnings()
{
return getWeeklySalary();
}
public String toString()
{
return String.format( "salaried employee: %s%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
}
}

************************HOURLYCLASS***********************

import java.util.Date;

public class HourlyEmployee extends Employee
{
private double wage;
private double hours;
public HourlyEmployee( String first, String last, String ssn, Date DayOfBirth, double hourlyWage, double hoursWorked )
{
super( first, last, ssn, DayOfBirth);
setWage( hourlyWage );
setHours( hoursWorked );
}

  
public final void setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
}
public double getWage()
{
return wage;
}
public final void setHours( double hoursWorked )
{
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.0;
}
public double getHours()
{
return hours;
}
public double earnings()
{
if ( getHours() <= 40 )
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
}
public String toString()
{
return String.format( "hourly employee: %s%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours() );
}
}

***************************************DATECLASS*********************************

package javaapplication23;
public class Date {
  
private int month;
private int day;
private int year;

private static final int[] daysPerMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};

public Date(int month, int day, int year){
if ( month <= 0 || month > 12)
throw new IllegalArgumentException("month (" + month + ") must be 1 = 12");
  
if(day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("day (" + day + ") out of range for the specified month and year");

if(month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 )))
throw new IllegalArgumentException("day (" + day + ") out of range for the specified month and year");

this.month = month;
this.day = day;
this.year = year;
}
public int getMonth(){
return month; }
public int getDay(){
return day; }
public int getYear(){
return year; }
public String toString() {
return String.format("%d %d %d", month, day, year);
}

}

**************************************COMMISSIONEDCLASS***********************************

package javaapplication23;

import java.util.Date;

public class CommissionEmployee extends Employee
{
private double grossSales;
private double commissionRate;
public CommissionEmployee( String first, String last, String ssn, Date DayOfBirth, double sales, double rate )
{
super (first, last, ssn, DayOfBirth);
setGrossSales( sales );
setCommissionRate( rate );
}

public final void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
}
public double getCommissionRate()
{
return commissionRate;
}
public final void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}
public double getGrossSales()
{
return grossSales;
}
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
public String toString()
{
return String.format( "%s: %s%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
}
}

*********************************BASEPLUSCOMMISSION CLASS*************************

package javaapplication23;

import java.util.Date;

public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;
public BasePlusCommissionEmployee( String first, String last,
String ssn, Date DayOfBirth,double sales, double rate, double salary )
{
super( first, last, ssn, DayOfBirth, sales, rate );
setBaseSalary( salary );
}
public final void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
}
public double getBaseSalary()
{
return baseSalary;
}
public double earnings()
{
return getBaseSalary() + super.earnings();
}
public String toString()
{
return String.format( "%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary() );
}
}

***********************************PIECEWORKER CLASS****************************************

import java.util.Date;

public class PieceWorker extends Employee {
private double wage;
private double pieces;
public PieceWorker( String first, String last, String ssn, Date DayOfBirth, double Wage, double Pieces )
{
super( first, last, ssn, DayOfBirth);
setWage( Wage );
setPieces( Pieces );
}
public final void setWage( double Wage )
{
wage = ( Wage < 0.0 ) ? 0.0 : Wage;
}
public double getWage()
{
return wage;
}
public final void setPieces( double Pieces )
{
pieces = ( ( Pieces >= 0.0 ) && ( Pieces <= 168.0 ) ) ?
Pieces : 0.0;
}
public double getPieces()
{
return pieces;
}
public double earnings()
{
return getWage() * getPieces();
}
public String toString()
{
return String.format( "Piece Worker: %s%s: $%,.2f; %s: %,.2f",
super.toString(), "Wage per piece", getWage(),
"Number of pieces produced", getPieces() );
}

}

***************************************FINAL OUTPUT SHOULD BE *************************************

Explanation / Answer

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

class PayrollSystemTest {
public static void main(String[] args) {
// create subclass objects
Date bdate = new Date(6,5,1944);
System.out.println(bdate);
SalariedEmployee salariedEmployee =
new SalariedEmployee(
"John", "Smith", "111-11-1111", bdate, 800.00);

Date date1 = new Date(12, 29, 1960);
System.out.println(date1);
HourlyEmployee hourlyEmployee =
new HourlyEmployee(
"Karen", "Price", "222-22-2222",date1 , 16.75, 40);


Date date2 = new Date(9, 8, 1954);
System.out.println(date2);
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333",date2 , 10000, .06);

Date date3 = new Date(3, 2, 1965);
System.out.println(date3);
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444",date3 , 5000, .04, 300);

Date date4 = new Date(11,11,1967);
System.out.println(date4);
PieceWorker pieceWorker =
new PieceWorker("James","Smith","555-55-5555",date4 , 25,33);
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 five-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());
}
}
}
//new provided code please look at is as it has changed
//********EMPLOYEE CLASS************
//import java.util.Date;
abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthday;
public Employee( String first, String last, String ssn , Date DayOfBirth)
{
this.firstName = first;
this.lastName = last;
this.socialSecurityNumber = ssn;
this.birthday = DayOfBirth;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn;
}
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
public Date getBirthDate()
{
return birthday;
}

public String toString()
{
return String.format( "%s %s social security number: %s birth date: %s ",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthDate().toString());
}
public abstract double earnings();
}
//*************SALARIEDCLASS*********
//import java.util.Date;
class SalariedEmployee extends Employee
{
private double weeklySalary;
public SalariedEmployee( String first, String last, String ssn, Date birthDate, double salary )
{
super( first, last, ssn, birthDate);
setWeeklySalary( salary );
}

public final void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public double earnings()
{
return getWeeklySalary();
}
public String toString()
{
return String.format( "salaried employee: %s%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
}
}
//************************HOURLYCLASS***********************
//import java.util.Date;
class HourlyEmployee extends Employee
{
private double wage;
private double hours;
public HourlyEmployee( String first, String last, String ssn, Date DayOfBirth, double hourlyWage, double hoursWorked )
{
super( first, last, ssn, DayOfBirth);
setWage( hourlyWage );
setHours( hoursWorked );
}

public final void setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
}
public double getWage()
{
return wage;
}
public final void setHours( double hoursWorked )
{
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.0;
}
public double getHours()
{
return hours;
}
public double earnings()
{
if ( getHours() <= 40 )
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
}
public String toString()
{
return String.format( "hourly employee: %s%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours() );
}
}
//***************************************DATECLASS*********************************
//package javaapplication23;
class Date {

private int month;
private int day;
private int year;

private static final int[] daysPerMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};

public Date(int month, int day, int year){
if ( month <= 0 || month > 12)
throw new IllegalArgumentException("month (" + month + ") must be 1 = 12");

if(day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("day (" + day + ") out of range for the specified month and year");
if(month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 )))
throw new IllegalArgumentException("day (" + day + ") out of range for the specified month and year");

this.month = month;
this.day = day;
this.year = year;
}
public int getMonth(){
return month; }
public int getDay(){
return day; }
public int getYear(){
return year; }
public String toString() {
return String.format(" Date object constructor for date %d-%d-%d", month, day, year);
}
}
//**************************************COMMISSIONEDCLASS***********************************
//package javaapplication23;
//import java.util.Date;
class CommissionEmployee extends Employee
{
private double grossSales;
private double commissionRate;
public CommissionEmployee( String first, String last, String ssn, Date DayOfBirth, double sales, double rate )
{
super (first, last, ssn, DayOfBirth);
setGrossSales( sales );
setCommissionRate( rate );
}

public final void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
}
public double getCommissionRate()
{
return commissionRate;
}
public final void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}
public double getGrossSales()
{
return grossSales;
}
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
public String toString()
{
return String.format( "%s: %s%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
}
}
//*********************************BASEPLUSCOMMISSION CLASS*************************
//package javaapplication23;
//import java.util.Date;
class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;
public BasePlusCommissionEmployee( String first, String last,
String ssn, Date DayOfBirth,double sales, double rate, double salary )
{
super( first, last, ssn, DayOfBirth, sales, rate );
setBaseSalary( salary );
}
public final void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
}
public double getBaseSalary()
{
return baseSalary;
}
public double earnings()
{
return getBaseSalary() + super.earnings();
}
public String toString()
{
return String.format( "%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary() );
}
}
//***********************************PIECEWORKER CLASS****************************************
//import java.util.Date;
class PieceWorker extends Employee {
private double wage;
private double pieces;
public PieceWorker( String first, String last, String ssn, Date DayOfBirth, double Wage, double Pieces )
{
super( first, last, ssn, DayOfBirth);
setWage( Wage );
setPieces( Pieces );
}
public final void setWage( double Wage )
{
wage = ( Wage < 0.0 ) ? 0.0 : Wage;
}
public double getWage()
{
return wage;
}
public final void setPieces( double Pieces )
{
pieces = ( ( Pieces >= 0.0 ) && ( Pieces <= 168.0 ) ) ?
Pieces : 0.0;
}
public double getPieces()
{
return pieces;
}
public double earnings()
{
return getWage() * getPieces();
}
public String toString()
{
return String.format( "Piece Worker: %s%s: $%,.2f; %s: %,.2f",
super.toString(), "Wage per piece", getWage(),
"Number of pieces produced", getPieces() );
}
}

Output:


Date object constructor for date 6-5-1944

Date object constructor for date 12-29-1960

Date object constructor for date 9-8-1954

Date object constructor for date 3-2-1965

Date object constructor for date 11-11-1967
Employees processed individually:

salaried employee: John Smith
social security number: 111-11-1111
birth date:
Date object constructor for date 6-5-1944
weekly salary: $800.00
earned: $800.00

hourly employee: Karen Price
social security number: 222-22-2222
birth date:
Date object constructor for date 12-29-1960
hourly wage: $16.75; hours worked: 40.00
earned: $670.00

commission employee: Sue Jones
social security number: 333-33-3333
birth date:
Date object constructor for date 9-8-1954
gross sales: $10,000.00; commission rate: 0.06
earned: $600.00

base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
birth date:
Date object constructor for date 3-2-1965
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
earned: $500.00

Piece Worker: James Smith
social security number: 555-55-5555
birth date:
Date object constructor for date 11-11-1967
Wage per piece: $25.00; Number of pieces produced: 33.00
earned: $825.00

Enter the current month (1 - 12):
Employees processed polymorphically:

salaried employee: John Smith
social security number: 111-11-1111
birth date:
Date object constructor for date 6-5-1944
weekly salary: $800.00
earned $800.00

hourly employee: Karen Price
social security number: 222-22-2222
birth date:
Date object constructor for date 12-29-1960
hourly wage: $16.75; hours worked: 40.00
earned $670.00 plus $100.00 birthday bonus

commission employee: Sue Jones
social security number: 333-33-3333
birth date:
Date object constructor for date 9-8-1954
gross sales: $10,000.00; commission rate: 0.06
earned $600.00

base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
birth date:
Date object constructor for date 3-2-1965
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
new base salary with 10% increase is: $330.00
earned $530.00

Piece Worker: James Smith
social security number: 555-55-5555
birth date:
Date object constructor for date 11-11-1967
Wage per piece: $25.00; Number of pieces produced: 33.00
earned $825.00

Employee 0 is a SalariedEmployee
Employee 1 is a HourlyEmployee
Employee 2 is a CommissionEmployee
Employee 3 is a BasePlusCommissionEmployee
Employee 4 is a PieceWorker

Do ask if any doubt. Please upvote

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote