Question: I have to write JAVA code. Please help me ASAP. Q. We added PieceWorker. Add BasePlusPieceWorker, a concept similar to BasePlusCommissionEmployee this time. ---------------------------------------- BasePlusCommissionEmployee.java

I have to write JAVA code. Please help me ASAP.

Q. We added PieceWorker. Add BasePlusPieceWorker, a concept similar to BasePlusCommissionEmployee this time.

----------------------------------------

BasePlusCommissionEmployee.java

public class BasePlusCommissionEmployee extends CommissionEmployee{

private double baseSalary;

// constructor

public BasePlusCommissionEmployee(String first, String last, String ssn,

double sales, double rate, double salary){

// call CommissionEmployee constructor

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

setBaseSalary(salary);

}

// SETTERS

public void setBaseSalary(double salary){

if(salary >= 0.0f)

baseSalary = salary;

else

throw new IllegalArgumentException(

"Base salary must be >= 0.0f");

}

// GETTERS

public double getBaseSalary(){

return this.baseSalary;

}

// calculate earnings; override method earnings in CommissionEmployee

@Override

public double earnings(){

return getBaseSalary() + super.earnings();

}

// return String representation of object

@Override

public String toString(){

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

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

"base salary", getBaseSalary());

}

}

CommissionEmployee.java

public class CommissionEmployee extends Employee{

private double grossSales;

private double commissionRate;

// constructor

public CommissionEmployee(String first, String last, String ssn,

double sales, double rate){

super(first, last, ssn);

setGrossSales(sales);

setCommissionRate(rate);

}

// SETTERS

public void setCommissionRate(double rate){

if(rate > 0.0f && rate < 1.0f)

this.commissionRate = rate;

else

throw new IllegalArgumentException(

"Commission rate must be > 0.0f and < 1.0f");

}

public void setGrossSales(double sales){

if(sales >= 0.0f)

this.grossSales = sales;

else

throw new IllegalArgumentException

("Gross sales muse be >= 0.0f");

}

// GETTERS

public double getCommissionRate(){

return this.commissionRate;

}

public double getGrossSales(){

return this.grossSales;

}

// calculate earnings; override abstract method earnings in Employee

@Override

public double earnings(){

return getCommissionRate() * getGrossSales();

}

// String representation of object

@Override

public String toString(){

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

"commission employee", super.toString(),

"gross sales", getGrossSales(),

"commission rate", getCommissionRate());

}

}

Employee.java

public abstract class Employee{

private final String firstName;

private final String lastName;

private final String socialSecurityNumber;

// constructor

public Employee(String firstName, String lastName, String socialSecurityNumber){

this.firstName = firstName;

this.lastName = lastName;

this.socialSecurityNumber = socialSecurityNumber;

}

public String getFirstName(){

return firstName;

}

public String getLastName(){

return lastName;

}

public String getSocialSecurityNumber(){

return socialSecurityNumber;

}

// return String representation of Employee object

@Override

public String toString(){

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

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

}

// ABSTRACT METHODS

// not implemented here

public abstract double earnings();

}

HourlyEmployee.java

public class HourlyEmployee extends Employee{

private double wage;

private double hours;

// constructor

public HourlyEmployee(String first, String last, String ssn,

double hourlyWage, double hoursWorked){

// explicit Employee constructor call

super(first, last, ssn);

setWage(hourlyWage);

setHours(hoursWorked);

}

// SETTERS

public void setWage(double hourlyWage){

if(hourlyWage >= 0.0f)

this.wage = hourlyWage;

else

throw new IllegalArgumentException(

"Hourly wage must be >= 0.0f");

}

public void setHours(double hoursWorked){

if((hoursWorked >= 0.0f) && (hoursWorked <= 168.0f))

this.hours = hoursWorked;

else

throw new IllegalArgumentException(

"Hours worked must be >= 0.0f and <= 16.0f");

}

// GETTERS

public double getWage(){

return this.wage;

}

public double getHours(){

return this.hours;

}

// calculate earnings; override abstract method earnings in Employee

@Override

public double earnings(){

if(getHours() <= 40)

return getWage() * getHours();

else

return 40 * getWage() + (getHours() - 40) * getWage() * 1.5f;

}

// String representation of object

@Override

public String toString(){

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

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

"hours worked", getHours());

}

}

PayrollSystemTest.java

public class PayrollSystemTest

{

public static void main( String args[] )

{

// create subclass objects

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

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;

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

} // end if

System.out.printf(

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

} // end for

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

PieceWorker Piece [] = new PieceWorker[3];

// initialize array with PieceWorker

Piece[ 0 ] = new PieceWorker( "John", "Smith", "111-11-1111",5,9);

Piece[ 1 ] = new PieceWorker( "Karen", "Price", "222-22-2222",8,15);

Piece[ 2 ] = new PieceWorker("Sue", "Jones", "333-33-3333",3,7);

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

System.out.printf( "%s %s: $%,.2f ",Piece[j], "earned", Piece[j].earnings() );

}

}

PieceWorker.java

public final class PieceWorker extends Employee {

private double wage ;

private int pieces ;

public PieceWorker( String first, String last,String ssn,double w, int P )

{

super( first, last,ssn );

setWage( w );

setPieces ( P );

}

public void setWage( double w )

{

if (w > 0)

wage = w ;

else

wage = 0;

}

public void setPieces( int P )

{

if(P > 0 )

pieces = P;

else

pieces = 0 ;

}

public double earnings()

{ return pieces * wage; }

public String toString()

{

return "Piece worker: " + super.toString();

}

}

SalariedEmployee.java

public class SalariedEmployee extends Employee{

private double weeklySalary;

// constructor

public SalariedEmployee(String first, String last, String ssn, double salary){

// pass to Employee constructor

super(first, last, ssn);

setWeeklySalary(salary);

}

// SETTERS

public void setWeeklySalary(double salary){

if(salary >= 0.0f)

this.weeklySalary = salary;

else

throw new IllegalArgumentException(

"Weekly salary must be >= 0.0f");

}

// GETTERS

public double getWeeklySalary(){

return this.weeklySalary;

}

// calculate earnings; override abstract method earnings in Employee

@Override

public double earnings(){

return getWeeklySalary();

}

// return String representation of SalriedEmployee object

@Override

public String toString(){

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

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

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!