In this lab assignment , we modify the payroll application. If the object curren
ID: 3744645 • Letter: I
Question
In this lab assignment , we modify the payroll application. If the object currently being processed is a BasePlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee’s base salary by 10%.
Complete the following steps to create the new application:
a) Modify classes HourlyEmployee and CommissionEmployee to place them in the IPayable hierarchy as derived classes of the version of Employee that implements IPayable. [Hint: Change the name of method Earnings to GetPaymentAmount in each derived class.]
b) Modify class BasePlusCommissionEmployee such that it extends the version of class CommissionEmployee created in Part a.
c) Modify PayableInterfaceTest to polymorphically process three salariedEmployee, two HourlyEmployee, one CommissionEmployee and two Base- PlusCommissionEmployee.
d) Create a menu that allows a user to select one of the following options to display a string representation of each IPayable objects.
Sort last name in descending order using IComparable
Sort pay amount in ascending order using IComparer
Sort by social security number in descending order using a selection sort and delegate
Sorting last name in ascending order and pay amount in descending order by using LINQ
Use the following data to test your program:
IPayable[] payableObjects = new IPayable[ 8 ];
payableObjects[ 0 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 700M );
payableObjects[1] = new SalariedEmployee("Antonio", "Smith", "555-55-5555", 800M);
payableObjects[2] = new SalariedEmployee("Victor", "Smith", "444-44-4444", 600M);
payableObjects[ 3 ] = new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75M, 40M );
payableObjects[4] = new HourlyEmployee("Ruben", "Zamora", "666-66-6666", 20.00M, 40M);
payableObjects[ 5 ] = new CommissionEmployee( "Sue", "Jones", "333-33-3333", 10000M, .06M );
payableObjects[ 6 ] = new BasePlusCommissionEmployee( "Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M );
payableObjects[7] = new BasePlusCommissionEmployee("Lee","Duarte", "888-88-888", 5000M, .04M, 300M);
Explanation / Answer
package ClassProg;
// Defines an interface IPayable
public interface IPayable
{
// Defines an inner class for exception handling
class ArgumentOutOfRangeException
{
// Parameterized constructor to display error message
ArgumentOutOfRangeException(String type, double value, String message)
{
System.out.println(type + " " + value + " " + message);
}// End of constructor
}// End of class
double GetPaymentAmount(); // calculate payment; no implementation
} // end interface IPayable
----------------------------------------------------------------------------
package ClassProg;
// Abstract class Employee implements interface IPayable
public abstract class Employee implements IPayable
{
// Instance variables to store data
private String firstName;
private String lastName;
private String SSN;
// read-only property that gets employee's first name
public String getFirstName()
{
return firstName;
}// End of method
// read-only property that gets employee's last name
public String getLastName()
{
return lastName;
}// End of method
// read-only property that gets employee's social security number
public String getSocialSecurityNumber()
{
return SSN;
}// End of method
// three-parameter constructor
public Employee(String first, String last, String ssn)
{
firstName = first;
lastName = last;
SSN = ssn;
} // end three-parameter Employee constructor
// return string representation of Employee object, using properties
public String toString()
{
return " First Name: " + firstName + " Last Name: " + lastName + " SSN: " + SSN;
} // end method ToString
} // end abstract class Employee
------------------------------------------------------------
package ClassProg;
// Class HourlyEmployee derived from abstract class Employee
public class HourlyEmployee extends Employee
{
private double wage; // wage per hour
private double hours; // hours worked for the week
// five-parameter constructor
public HourlyEmployee(String first, String last, String ssn, double hourlyWage, double hoursWorked)
{
super( first, last, ssn);
wage = hourlyWage; // validate hourly wage via property
hours = hoursWorked; // validate hours worked via property
} // end five-parameter HourlyEmployee constructor
// property that gets and sets hourly employee's wage
public double getWage()
{
return wage;
} // end get
public void setWage(double value)
{
if ( value >= 0 ) // validation
wage = value;
else
new ArgumentOutOfRangeException( "Wage", value, "Wage must be >= 0" );
} // end set
// property that gets and sets hourly employee's hours
public double getHours()
{
return hours;
} // end get
public void setHours(double value)
{
if ( value >= 0 && value <= 168 ) // validation
hours = value;
else
new ArgumentOutOfRangeException( "Hours", value, "Hours must be >= 0 and <= 168" );
} // end set
// calculate earnings; override Employee’s abstract method Earnings
public double GetPaymentAmount()
{
if ( hours <= 40 ) // no overtime
return wage * hours;
else
return ( 40 * wage ) + ( ( hours - 40 ) * wage * 1.5);
} // end method Earnings
// return string representation of HourlyEmployee object
public String toString()
{
return super.toString() + " Hourly Wage: " + wage + " Hours Worked: " + hours;
} // end method ToString
} // end class HourlyEmployee
-------------------------------------------------------------------------
package ClassProg;
// Class CommissionEmployee derived from abstract class Employee
public class CommissionEmployee extends Employee
{
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-parameter constructor
public CommissionEmployee(String first, String last, String ssn, double sales, double rate)
{
super( first, last, ssn );
grossSales = sales; // validate gross sales via property
commissionRate = rate; // validate commission rate via property
} // end five-parameter CommissionEmployee constructor
// property that gets and sets commission employee's gross sales
public double getGrossSales()
{
return grossSales;
} // end get
public void setGrossSales(double value)
{
if ( value >= 0 )
grossSales = value;
else
new ArgumentOutOfRangeException("GrossSales", value, "GrossSales must be >= 0" );
} // end set
// property that gets and sets commission employee's commission rate
public double getCommissionRate()
{
return commissionRate;
} // end get
public void setCommissionRate(double value)
{
if ( value > 0 && value < 1 )
commissionRate = value;
else
new ArgumentOutOfRangeException( "CommissionRate", value, "CommissionRate must be > 0 and < 1" );
}// end property CommissionRate
// calculate earnings; override abstract method Earnings in Employee
public double GetPaymentAmount()
{
return commissionRate * grossSales;
} // end method Earnings
// return string representation of CommissionEmployee object
public String toString()
{
return super.toString() + " Gross Sales: " + grossSales + " Commission Rate: " + commissionRate;
} // end method ToString
}// End of class CommissionEmployee
---------------------------------------------------------------------------
package ClassProg;
// Class BasePlusCommissionEmployee derived from class CommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week
// six-parameter constructor
public BasePlusCommissionEmployee(String first, String last, String ssn, double sales, double rate, double salary)
{
super(first, last, ssn, sales, rate);
baseSalary = salary; // validate base salary via property
} // end six-parameter BasePlusCommissionEmployee constructor
// property that gets and sets
// base-salaried commission employee's base salary
public double getBaseSalary()
{
return baseSalary;
} // end get
public void setBaseSalary(double value)
{
if ( value >= 0 )
baseSalary = value;
else
new ArgumentOutOfRangeException( "BaseSalary", value, "BaseSalary must be >= 0" );
} // end set
// calculate earnings; override method Earnings in CommissionEmployee
public double GetPaymentAmount()
{
return baseSalary;
} // end method Earnings
// return string representation of BasePlusCommissionEmployee object
public String toString()
{
return super.toString() + " Base Salary: " + baseSalary;
} // end method ToString
} // end class BasePlusCommissionEmployee
----------------------------------------------------------------
package ClassProg;
// Class SalariedEmployee derived from abstract class Employee
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-parameter constructor
public SalariedEmployee(String first, String last, String ssn, double salary)
{
super(first, last, ssn);
weeklySalary = salary; // validate salary via property
} // end four-parameter SalariedEmployee constructor
// property that gets and sets salaried employee's salary
public double getWeeklySalary()
{
return weeklySalary;
} // end get
public void setWeeklySalary(double value)
{
if ( value >= 0 ) // validation
weeklySalary = value;
else
new ArgumentOutOfRangeException( "WeeklySalary", value, "WeeklySalary must be >= 0" );
} // end set
// calculate earnings; override abstract method Earnings in Employee
public double GetPaymentAmount()
{
return weeklySalary;
} // end method Earnings
// return string representation of SalariedEmployee object
public String toString()
{
return super.toString() + " Weekly Salary: " + weeklySalary;
} // end method ToString
} // end class SalariedEmployee
------------------------------------------------------------------------
package ClassProg;
// Driver class PayrollSystemTest definition
public class PayrollSystemTest
{
// main function definition
public static void main(String[] args )
{
// create derived class objects
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40.0);
CommissionEmployee commissionEmployee =
new CommissionEmployee( "Sue", "Jones", "333-33-3333", 10000.00, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee( "Bob", "Lewis", "444-44-4444", 5000.00, .04, 300.00);
System.out.println("Employees processed individually: ");
System.out.println(salariedEmployee);
System.out.println(hourlyEmployee);
System.out.println(commissionEmployee);
System.out.println(basePlusCommissionEmployee);
// create four-element Employee array
Employee[] employees = new Employee[4];
// initialize array with Employees of derived types
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;
employee.setBaseSalary(employee.getBaseSalary() * 1.10);
System.out.format("New base salary with 10%% increase is: %.2f", employee.getBaseSalary());
} // end if
System.out.println(" Paymnent: " + currentEmployee.GetPaymentAmount());
} // end foreach
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++ )
{
System.out.printf(" Employee %d is a: ", j);
System.out.print(employees[j].getClass());
}
} // end Main
} // end class PayrollSystemTest
Sample output:
Employees processed individually:
First Name: John
Last Name: Smith
SSN: 111-11-1111
Weekly Salary: 800.0
First Name: Karen
Last Name: Price
SSN: 222-22-2222
Hourly Wage: 16.75
Hours Worked: 40.0
First Name: Sue
Last Name: Jones
SSN: 333-33-3333
Gross Sales: 10000.0
Commission Rate: 0.06
First Name: Bob
Last Name: Lewis
SSN: 444-44-4444
Gross Sales: 5000.0
Commission Rate: 0.04
Base Salary: 300.0
Employees processed polymorphically:
First Name: John
Last Name: Smith
SSN: 111-11-1111
Weekly Salary: 800.0
Paymnent: 800.0
First Name: Karen
Last Name: Price
SSN: 222-22-2222
Hourly Wage: 16.75
Hours Worked: 40.0
Paymnent: 670.0
First Name: Sue
Last Name: Jones
SSN: 333-33-3333
Gross Sales: 10000.0
Commission Rate: 0.06
Paymnent: 600.0
First Name: Bob
Last Name: Lewis
SSN: 444-44-4444
Gross Sales: 5000.0
Commission Rate: 0.04
Base Salary: 300.0
New base salary with 10% increase is: 330.00
Paymnent: 330.0
Employee 0 is a: class ClassProg.SalariedEmployee
Employee 1 is a: class ClassProg.HourlyEmployee
Employee 2 is a: class ClassProg.CommissionEmployee
Employee 3 is a: class ClassProg.BasePlusCommissionEmployee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.