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

Modify the payroll system to include an additional Employee subclass PieceWorker

ID: 3822160 • Letter: M

Question

Modify the payroll system to 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 objects of each concrete class in the new Employee hierarchy. For each Employee, display its string representation and earnings. This is a java program.

In this exercise, we modify the accounts payable application to include the complete functionality of the payroll application. The application should still process two invoice objects, but now should process one object of each of the four Employee subclasses. If the object currently being processed is aBasePlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee’s base salary by 10%.Finally, the application should output the payment amount for each object.Complete the following steps to create the new application: a) Modify classes HourlyEmployee and CommissionEmployee to place them in the Payable hierarchy as subclasses of the version of Employee that implements Payable. [Hint: You should introduce a getPaymentAmount method in each subclass so that the class satisfies its inherited contract with interface Payable. The getPaymentAmount can call earning to calculate the payable salary. b) Modify class BasePlusCommissionEmployee such that it extends the version of class CommissionEmployee created in Part a. c) Modify PayableInterfaceTest to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, oneCommissionEmployee and one BasePlusCommissionEmployee. First output a string representation of each Payable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each Payable object.

Code so far:

Driver Class:

public class PayrollSystemTest
{
public static void main( String args[] )
{
   SalariedEmployee salariedEmployee =
       new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00);
       HourlyEmployee hourlyEmployee =
       new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40);
CommissionEmployee commissionEmployee =
       new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee =
       new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000, .04, 300);
   Employee employees[] = new Employee[ 4 ];
   employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
System.out.println( "Employees processed polymorphically:" );

for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee );
if ( currentEmployee instanceof BasePlusCommissionEmployee )
{
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() );
}
System.out.printf(
"earned $%,.2f ", currentEmployee.earnings() );
}
}
}

Employee Class:

//Employee abstract superclass.
public abstract class Employee


{


private String firstName;


private String lastName;


private String socialSecurityNumber;


  


// three-argument constructor


public Employee( String first, String last, String ssn)


{


firstName = first;


lastName = last;


socialSecurityNumber = ssn;


} // end three-argument Employee constructor


// set first name


public void setFirstName( String first )


{


firstName = first;


} // end method setFirstName


// return first name


public String getFirstName()


{


return firstName;


} // end method getFirstName


// set last name


public void setLastName( String last )


{


lastName = last;


} // 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 String toString()


{


return String.format( "%s %s social security number: %s"+" ",


getFirstName(), getLastName(), getSocialSecurityNumber());

} // end method toString


// abstract method overridden by subclasses


public abstract double earnings(); // no implementation here


} // end abstract class Employee

Commission Employee Class:

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,


double sales, double rate)


{


super( first, last, ssn);


setGrossSales( sales );


setCommissionRate( rate );


} // end five-argument CommissionEmployee constructor


// set commission rate


public void setCommissionRate( double rate )


{


commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;


} // end method setCommissionRate


// return commission rate


public double getCommissionRate()


{


return commissionRate;


} // end method getCommissionRate


// set gross sales amount


public void setGrossSales( double sales )


{


grossSales = ( sales < 0.0 ) ? 0.0 : sales;


} // end method setGrossSales


// return gross sales amount


public double getGrossSales()


{


return grossSales;


} // end method getGrossSales


// calculate earnings; override abstract method earnings in Employee


public double earnings()


{


return getCommissionRate() * getGrossSales();


} // end method earnings


// return String representation of CommissionEmployee object


public String toString()


{


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


"commission employee", super.toString(),


"gross sales", getGrossSales(),


"commission rate", getCommissionRate() );


} // end method toString


} // end class CommissionEmployee

Base Plus Commission Employee Class:

public class BasePlusCommissionEmployee extends CommissionEmployee


{


private double baseSalary; // base salary per week


// six-argument constructor


public BasePlusCommissionEmployee( String first, String last,


String ssn, double sales, double rate, double salary)


{


super( first, last, ssn, sales, rate);


setBaseSalary( salary ); // validate and store base salary


} // end six-argument BasePlusCommissionEmployee constructor


// set base salary


public void setBaseSalary( double salary )


{


baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative


} // end method setBaseSalary


// return base salary


public double getBaseSalary()


{


return baseSalary;


} // end method getBaseSalary


// calculate earnings; override method earnings in CommissionEmployee


public double earnings()


{


return getBaseSalary() + super.earnings();


} // end method earnings


// return String representation of BasePlusCommissionEmployee object


public String toString()


{


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


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


"base salary", getBaseSalary() );


} // end method toString


} // end class BasePlusCommissionEmployee

Hourly Employee Class:

//HourlyEmployee class extends Employee.


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,


double hourlyWage, double hoursWorked)


{


super( first, last, ssn);


setWage( hourlyWage ); // validate and store hourly wage


setHours( hoursWorked ); // validate and store hours worked


} // end five-argument HourlyEmployee constructor


// set wage


public void setWage( double hourlyWage )


{


wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;


} // end method setWage


// return wage


public double getWage()


{


return wage;


} // end method getWage


// set hours worked


public void setHours( double hoursWorked )


{


hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?


hoursWorked : 0.0;


} // end method setHours


// return hours worked


public double getHours()


{


return hours;


} // end method getHours


// calculate earnings; override abstract method earnings in Employee


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


public String toString()


{


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


super.toString(), "hourly wage", getWage(),


"hours worked", getHours() );


} // end method toString


} // end class HourlyEmployee

Salaried Employee:

//SalariedEmployee class extends Employee.
public class SalariedEmployee extends Employee


{


private double weeklySalary;


// four-argument constructor


public SalariedEmployee( String first, String last, String ssn,


double salary)


{


super( first, last, ssn); // pass to Employee constructor


setWeeklySalary( salary ); // validate and store salary


} // end four-argument SalariedEmployee constructor


// set salary


public void setWeeklySalary( double salary )


{


weeklySalary = salary < 0.0 ? 0.0 : salary;


} // end method setWeeklySalary


// return salary


public double getWeeklySalary()


{


return weeklySalary;


} // end method getWeeklySalary


// calculate earnings; override abstract method earnings in Employee


public double earnings()


{


return getWeeklySalary();


} // end method earnings


// return String representation of SalariedEmployee object


public String toString()


{


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


super.toString(), "weekly salary", getWeeklySalary() );


} // end method toString


} // end class SalariedEmployee

Pieceworker Class Template:

// Problem 1: PieceWorker.java
// PieceWorker class extends Employee.

public class PieceWorker extends Employee
{
/* declare instance variable wage */
/* declare instance variable pieces */

// five-argument constructor
public PieceWorker( String first, String last, String ssn,
double wagePerPiece, int piecesProduced )
{
/* write code to initialize a PieceWorker */
} // end five-argument PieceWorker constructor

// set wage
/* write a set method that validates and sets the PieceWorker's wage */

// return wage
/* write a get method that returns the PieceWorker's wage */

// set pieces produced
/* write a set method that validates and sets the number of pieces produced */

// return pieces produced
/* write a get method that returns the number of pieces produced */

// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
/* write code to return the earnings for a PieceWorker */
} // end method earnings

// return String representation of PieceWorker object
public String toString()
{
/* write code to return a string representation of a PieceWorker */
} // end method toString
} // end class PieceWorker

Invoice Class Template:

// Problem 2: Invoice.java
// Invoice class implements Payable.

public class Invoice implements Payable
{
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;

// four-argument constructor
public Invoice( String part, String description, int count,
double price )
{
partNumber = part;
partDescription = description;
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store price per item
} // end four-argument Invoice constructor

// set part number
public void setPartNumber( String part )
{
partNumber = part;
} // end method setPartNumber

// get part number
public String getPartNumber()
{
return partNumber;
} // end method getPartNumber

// set description
public void setPartDescription( String description )
{
partDescription = description;
} // end method setPartDescription

// get description
public String getPartDescription()
{
return partDescription;
} // end method getPartDescription

// set quantity
public void setQuantity( int count )
{
quantity = ( count < 0 ) ? 0 : count; // quantity cannot be negative
} // end method setQuantity

// get quantity
public int getQuantity()
{
return quantity;
} // end method getQuantity

// set price per item
public void setPricePerItem( double price )
{
pricePerItem = ( price < 0.0 ) ? 0.0 : price; // validate price
} // end method setPricePerItem

// get price per item
public double getPricePerItem()
{
return pricePerItem;
} // end method getPricePerItem

// return String representation of Invoice object
public String toString()
{
return String.format( "%s: %s: %s (%s) %s: %d %s: $%,.2f",
"invoice", "part number", getPartNumber(), getPartDescription(),
"quantity", getQuantity(), "price per item", getPricePerItem() );
} // end method toString

// method required to carry out contract with interface Payable
public double getPaymentAmount()
{
return getQuantity() * getPricePerItem(); // calculate total cost
} // end method getPaymentAmount
} // end class Invoice

Payable Template:

// Problem 2: Payable.java
// Payable interface declaration.

public interface Payable
{
double getPaymentAmount(); // calculate payment; no implementation
} // end interface Payable

PayableInterface Test Template:

// Problem 2: PayableInterfaceTest.java
// Tests interface Payable.

public class PayableInterfaceTest
{
public static void main( String args[] )
{
// create six-element Payable array
Payable payableObjects[] = new Payable[ 6 ];
  
// populate array with objects that implement Payable
payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
payableObjects[ 2 ] =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
payableObjects[ 3 ] =
/* create an HourlyEmployee object */
payableObjects[ 4 ] =
/* create a CommissionEmployee object */
payableObjects[ 5 ] =
/* create a BasePlusCommissionEmployee object */

System.out.println(
"Invoices and Employees processed polymorphically: " );

// generically process each element in array payableObjects
for ( Payable currentPayable : payableObjects )
{
// output currentPayable and its appropriate payment amount
System.out.printf( "%s ", currentPayable.toString() );
  
/* write code to determine whether currentPayable is a
BasePlusCommissionEmployee object */
{
/* write code to give a raise */
/* write code to ouput results of the raise */
} // end if

System.out.printf( "%s: $%,.2f ",
"payment due", currentPayable.getPaymentAmount() );
} // end for
} // end main
} // end class PayableInterfaceTest

Explanation / Answer

Here is the code for the above scenario:

Date.java

package payrollsystem;

public class Date

{

private int month;

private int day;  

private int year;

public Date( int theMonth, int theDay, int theYear )

{

month = theMonth;

year = theYear;

day = checkDay( theDay );

}

public void setMonth(int theMonth)

{

month = theMonth;

}

public int getMonth()

{

if ( month > 0 && month <= 12 )

return month;

return 0;

}

private int checkDay( int testDay )

{

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

if ( testDay > 0 && testDay <= daysPerMonth[ month ] )

return testDay;

if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||

( year % 4 == 0 && year % 100 != 0 ) ) )

return testDay;

return 0;

}

public String toString()

{

return String.format( "%d/%d/%d", month, day, year );

}

}

Employee.java

package payrollsystem;

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)

{

firstName = first;

lastName = last;

socialSecurityNumber = ssn;

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 void setBirthday(Date DayOfBirth)

{

birthday = DayOfBirth;

}

public Date getBirthday()

{

return birthday;

}

public String toString()

{

return String.format( "%s %s social security number: %s birthday: %s ",

getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthday());

}

public abstract double earnings();

}

HourlyEmployee.Java

package payrollsystem;

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

}

}

SalariedEmployee.java

package payrollsystem;

public class SalariedEmployee extends Employee

{

private double weeklySalary;

public SalariedEmployee( String first, String last, String ssn, Date DayOfBirth, double salary )

{

super( first, last, ssn, DayOfBirth);

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

}

}

CommissionEmployee.java

package payrollsystem;

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

}

}

BasePlusCommissionEmployee.java

package payrollsystem;

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;

}

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.java

               

package payrollsystem;

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

}

}

PayrollSystemTest.java

               

package payrollsystem;

public class PayrollSystemTest

{

public static void main( String args[] )

{

Date currentDate = new Date(6,20,2010);

System.out.printf( "Current Date is: %s ", currentDate.toString() );

System.out.println("###################################");

SalariedEmployee salariedEmployee =

new SalariedEmployee( "John", "Smith", "111-11-1111", new Date(5,11,1984),800.00 );

HourlyEmployee hourlyEmployee =

new HourlyEmployee( "Karen", "Price", "222-22-2222", new Date(6,15,1988),16.75, 40 );

CommissionEmployee commissionEmployee =

new CommissionEmployee(

"Sue", "Jones", "333-33-3333", new Date(8,25,1974),10000, .06 );

BasePlusCommissionEmployee basePlusCommissionEmployee =

new BasePlusCommissionEmployee(

"Bob", "Lewis", "444-44-4444", new Date(9,27,1978),5000, .04, 300 );

PieceWorker pieceWorker = new PieceWorker("Ralph", "Tang", "777-223-987",

new Date(6,25,1985), 213, 16);

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

Employee employees[] = new Employee[ 5 ];

employees[ 0 ] = salariedEmployee;

employees[ 1 ] = hourlyEmployee;

employees[ 2 ] = commissionEmployee;

employees[ 3 ] = basePlusCommissionEmployee;

employees[ 4 ] = pieceWorker;

System.out.println("###################################");

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

for ( Employee currentEmployee : employees )

{

System.out.println( currentEmployee );

if ( currentEmployee instanceof BasePlusCommissionEmployee )

{

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(currentDate.getMonth()==currentEmployee.getBirthday().getMonth())

{

System.out.printf("earned $%,.2f. %s ", currentEmployee.earnings() + 100.00,

"Note: added a $100 bonus to your payroll amount for birthday!!!" );

}else{

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

}

}

for ( int j = 0; j < employees.length; j++ )

System.out.printf( "Employee %d is a %s ", j,

employees[ j ].getClass().getSimpleName() );

}

}

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