Homework Solution: In Chapter 9, we studied an inheritance hierarchy in which class BasePlusCommissionEmployee…

    In Chapter 9, we studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from CommissionEmployee. However, not all employees are CommissionedEmployees. For this assignment, create a more general Employee class that factors out the attributes and behavior that are common to all employees. Also, you will add two new types of employees, a SalariedEmployee, who gets paid a fixed salary, and an HourlyEmployee who gets paid an hourly wage plus time-and-a-half (1.5 times their hourly wage for hours worked over 40 hours). Create the proper Employee class hierarchy to reflect good object-oriented design. The SalariedEmployee will need the additional instance variable of salary, and the HourlyEmployee will need the additional instance variables of hourlyWage and hoursWorked. Add setters and getters for these classes as appropriate. These classes will also need the earnings method and an override for the toString() method. The SalariedEmployee class will need a setSalary method to set the current salary for the employee. The HourlyEmployee class will need set methods for the hourlyWage and hoursWorked variables also. Salary and hourlyWage should be checked to make sure they are greater than zero and hoursWorked should be checked to ensure that it is between 1 and 168 (the number of hours in a week). Your code in the subclasses should call methods in the super classes whenever possible to reduce the amount of code in the subclasses and utilize the code already developed in the super classes as in the code demonstrated in Figures 9.10 and 9.11 in the book. Use the following code in your main function to test your classes: CommissionEmployee employee1 = new CommissionEmployee("Fred", "Jones", "111-11-1111", 2000.0, .05); BasePlusCommissionEmployee employee2 = new BasePlusCommissionEmployee("Sue", "Smith", "222-22-2222", 3000.0, .05, 300); SalariedEmployee employee3 = new SalariedEmployee("Sha", "Yang", "333-33-3333", 1150.0); HourlyEmployee employee4 = new HourlyEmployee("Ian", "Tanning", "444-44-4444", 15.0, 50); HourlyEmployee employee5 = new HourlyEmployee("Angela", "Domchek", "555-55-5555", 20.0, 40); System.out.printf("%s%s%s%s%s", employee1, employee2, employee3, employee4, employee5); The output from your program should look like the following: run: Commissioned Employee: Fred Jones with ssn: 111-11-1111 Gross Sales: 2000.00 Commission Rate: 0.05 Earnings: $100.00 Base Salary Plus Commissioned Employee: Sue Smith with ssn: 222-22-2222 Gross Sales: 3000.00 Commission Rate: 0.05 with Base Salary of: $300.00 Earnings: $450.00 Salaried Employee: Sha Yang with ssn: 333-33-3333 Salary: 1150.00 Earnings: $1150.00 Hourly Employee: Ian Tanning with ssn: 444-44-4444 Hourly Wage: 15.00 Hours Worked: 50.00 Earnings: $825.00 Hourly Employee: Angela Domchek with ssn: 555-55-5555 Hourly Wage: 20.00 Hours Worked: 40.00 Earnings: $800.00

    Expert Answer

     
    driver.java public class driver { public static void main(String[] args) {

    In Chapter 9, we thought-quenched an legacy hierarchy in which dispose BasePlusCommissionEmployee inherited from CommissionEmployee. However, referable perfect employees are CommissionedEmployees. Control this ordinance, shape a further public Employee dispose that factors quenched the attributes and deportment that are despicable to perfect employees. Besides, you conciliate infer brace odd types of employees, a SalariedEmployee, who secures hired a unwandering allowance, and an HourlyEmployee who secures hired an hourly wage plus time-and-a-half (1.5 times their hourly wage control hours worked aggravate 40 hours). Shape the appertinent Employee dispose hierarchy to heed good-tempered-tempered object-oriented plan.

    The SalariedEmployee conciliate want the inferitional solicitation wavering of allowance, and the HourlyEmployee conciliate want the inferitional solicitation waverings of hourlyWage and hoursWorked. Infer determinedters and secureters control these disposees as misappropriate. These disposees conciliate besides want the hues systematicity and an aggravateride control the toString() systematicity. The SalariedEmployee dispose conciliate want a determinedAllowance systematicity to determined the ordinary allowance control the employee. The HourlyEmployee dispose conciliate want determined systematicitys control the hourlyWage and hoursWorked waverings besides. Allowance and hourlyWage should be checked to shape permanent they are senior than naught and hoursWorked should be checked to enpermanent that it is among 1 and 168 (the compute of hours in a week).

    Your method in the subclasses should cperfect systematicitys in the super disposees whenever potential to refer the equality of method in the subclasses and husband the method already unconcealed in the super disposees as in the method demonstrated in Figures 9.10 and 9.11 in the work.

    Use the aftercited method in your ocean character to touchstone your disposees:

    CommissionEmployee employee1 = odd CommissionEmployee(“Fred”, “Jones”, “111-11-1111”, 2000.0, .05);
    BasePlusCommissionEmployee employee2 = odd BasePlusCommissionEmployee(“Sue”, “Smith”, “222-22-2222”, 3000.0, .05, 300);
    SalariedEmployee employee3 = odd SalariedEmployee(“Sha”, “Yang”, “333-33-3333”, 1150.0);
    HourlyEmployee employee4 = odd HourlyEmployee(“Ian”, “Tanning”, “444-44-4444”, 15.0, 50);
    HourlyEmployee employee5 = odd HourlyEmployee(“Angela”, “Domchek”, “555-55-5555”, 20.0, 40);
    System.out.printf(“%s%s%s%s%s”, employee1, employee2, employee3, employee4, employee5);

    The quenchedput from your program should face relish the aftercited:

    run:
    Commissioned Employee: Fred Jones with ssn: 111-11-1111
    Gross Sales: 2000.00
    Commission Rate: 0.05
    Earnings: $100.00
    Base Allowance Plus Commissioned Employee: Sue Smith with ssn: 222-22-2222
    Gross Sales: 3000.00
    Commission Rate: 0.05
    with Base Allowance of: $300.00
    Earnings: $450.00
    Salaried Employee: Sha Yang with ssn: 333-33-3333
    Salary: 1150.00
    Earnings: $1150.00
    Hourly Employee: Ian Tanning with ssn: 444-44-4444
    Hourly Wage: 15.00
    Hours Worked: 50.00
    Earnings: $825.00
    Hourly Employee: Angela Domchek with ssn: 555-55-5555
    Hourly Wage: 20.00
    Hours Worked: 40.00
    Earnings: $800.00

    Expert Confutation

     

    driver.java
    public dispose driver {

    public static lacking ocean(String[] args) {

    embrace benefit=0.02;

    CommissionEmployee employee1 = odd CommissionEmployee(“Fred”, “Jones”, “111-11-1111”, 2000.0, .05);
    BasePlusCommissionEmployee employee2 = odd BasePlusCommissionEmployee(“Sue”, “Smith”, “222-22-2222”, 3000.0, .05, 300);
    SalariedEmployee employee3 = odd SalariedEmployee(“Sha”, “Yang”, “333-33-3333”, 1150.0);
    HourlyEmployee employee4 = odd HourlyEmployee(“Ian”, “Tanning”, “444-44-4444”, 15.0, 50);
    HourlyEmployee employee5 = odd HourlyEmployee(“Angela”, “Domchek”, “555-55-5555”, 20.0, 40);

    Employee employees[]={employee1, employee2,employee3, employee4,
    employee5};

    System.out.println(“Employee info (pre-raise): “);
    control (Employee ordinary: employees)
    System.out.println(current);

    System.out.println(“——————————“);

    System.out.println(“Employee info (post-raise):”);
    control (Employee ordinary: employees){
    if(ordinary solicitationof SalariedEmployee)
    current.raise(0.04);

    else
    current.raise(bonus);

    System.out.println(current);

    }

    System.out.printf(“%s%s%s%s%s”, employee1, employee2, employee3, employee4, employee5);

    }

    }
    SalariedEmployee.java

    /*
    * To diversify this allow header, adopt Allow Headers in Project Appertinentties.
    * To diversify this template finish, adopt Tools | Templates
    * and unconcealed the template in the editor.
    */

    package employee;

    import java.text.NumberFormat;

    /**
    *
    * @author Robert
    */
    public dispose SalariedEmployee extends Employee{
    private embrace allowance;

    public SalariedEmployee(String firstName, String lastName, String SSN,
    embrace allowance){

    super(firstName,lastName,SSN);

    if(salary<=0)
    throw odd IllegalArgumentException(“How did you referable secure everything.”);

    this.salary=salary;
    }

    public lacking determinedSalary(embrace val){
    if (val<=0)
    throw odd IllegalArgumentException(“Allowance canreferable be 0 or less”);

    salary=val;
    }

    public embrace secureSalary(){
    return allowance;
    }

    @Override
    public embrace calcEarn(){
    //setSalary(salary); referable necessary

    return secureSalary();
    }

    @Override
    //precond-perc is entered as a decimal
    public lacking educate(embrace perc){
    if(perc>=1)
    throw odd IllegalArgumentException(“Percentage must be infered ”
    + “as a decimal.”);

    salary=salary*(1+perc);
    }

    @Override
    public String toString(){
    return String.format(“%s: %s %s %s: %s%n %s: %.2f%n %s %s%n”,
    “Salaried Employee”, secureFirstName(), secureLastName(),”with ssn”,
    getSSN(),”Salary”,getSalary(), “Earnings:”, ComputeFormat.getCurrencyInstance().format(calcEarn()));
    }

    }

    Employee.java

    /*
    * To diversify this allow header, adopt Allow Headers in Project Appertinentties.
    * To diversify this template finish, adopt Tools | Templates
    * and unconcealed the template in the editor.
    */
    package employee;
    import java.text.NumberFormat;
    /**
    *
    * @author rjy5060
    */
    public unembodied dispose Employee{

    private decisive String firstName;
    private decisive String lastName;
    private decisive String SSN;
    //private embrace acquire; //Decided athwart using an acquire wavering

    public Employee(String firstName, String lastName, String SSN){
    this.firstName=firstName;
    this.lastName=lastName;
    this.SSN=SSN;
    }

    public String secureFirstName(){
    return firstName;
    }

    public String secureLastName(){
    return lastName;
    }

    public String secureSSN(){
    return SSN;
    }

    public unembodied embrace calcEarn();

    public unembodied lacking educate(embrace perc);

    public String toString(){
    return String.format(“%s: %s %s %n %s %s %n”,
    “Employee: “, secureFirstName(), secureLastName(), “SSN: “, secureSSN());
    }

    }

    CommissionEmployee.java

    import java.text.NumberFormat;
    public dispose CommissionEmployee extends Employee{
    private embrace grossSales;
    private embrace comRate;

    public CommissionEmployee(String firstName, String lastName, String SSN,
    embrace grossSales, embrace comRate){
    super(firstName, lastName, SSN);

    if(grossSales<=0.0)
    throw odd IllegalArgumentException(“Sales canreferable be 0.”);

    if(comRate<=0.0||comRate>=1.0)
    throw odd IllegalArgumentException(“CommissionRate canreferable be 0 and canreferable be”
    + “senior than 1.”);

    this.grossSales=grossSales;
    this.comRate=comRate;
    }

    public embrace secureGrossSales(){
    return grossSales;
    }

    public lacking determinedGrossSales(embrace val){
    if(val<=0)
    throw odd IllegalArgumentException(“No sales, what a loser”);

    this.grossSales=val;
    }

    public embrace secureComRate(){
    return comRate;
    }

    public lacking determinedComRate(embrace val){
    if(val<=0)
    throw odd IllegalArgumentException(“Eyyyy no point?”);

    this.comRate=val;
    }

    @Override
    public embrace calcEarn(){
    return secureGrossSales()*getComRate();
    }

    @Override
    //precond-perc is entered as a decimal
    public lacking educate(embrace perc){
    if(perc>=1)
    throw odd IllegalArgumentException(“Percentage must be infered ”
    + “as a decimal.”);

    comRate=(1+perc)*comRate;
    }

    @Override
    public String toString(){
    return String.format(“%s: %s %s %s: %s%n %s: %.2f%n %s: %.2f%n %s %s%n”,
    “Commission Employee”, secureFirstName(), secureLastName(),”with ssn”,
    getSSN(),”Gross Sales”, secureGrossSales(), “Commission Rate”,
    getComRate(), “Earnings:”, ComputeFormat.getCurrencyInstance().format(calcEarn()));
    }

    }

    BasePlusCommissionEmployee.java

    import java.text.NumberFormat;

    public dispose BasePlusCommissionEmployee extends CommissionEmployee{
    private embrace baseSalary;

    public BasePlusCommissionEmployee(String firstName, String lastName, String SSN,
    embrace grossSales, embrace comRate, embrace baseSalary){

    super(firstName, lastName, SSN, grossSales, comRate);
    this.baseSalary=baseSalary;
    }

    public embrace secureBaseSal(){
    return baseSalary;
    }
    public lacking determinedBaseSal(embrace val){
    if(val<=0)
    throw odd IllegalArgumentException(“What a loser, no sales”);

    this.baseSalary=val;
    }

    @Override
    public embrace calcEarn(){
    return secureGrossSales()*getComRate()+getBaseSal();
    }

    @Override
    //precond-perc is entered as a decimal
    public lacking educate(embrace perc){
    if(perc>=1)
    throw odd IllegalArgumentException(“Percentage must be infered ”
    + “as a decimal.”);

    setComRate((1+perc)*getComRate());
    baseSalary=(1+perc)*baseSalary;

    }

    @Override
    public String toString(){
    return String.format(“%s: %s %s %s: %s%n %s: %.2f%n %s: %.2f%n %s%.2f%n %s%s%n”,
    “Base Allowance + Commission Employee”, secureFirstName(), secureLastName(),”with ssn”,
    getSSN(),”Gross Sales”, secureGrossSales(), “Commission Rate”,
    getComRate(), “with Base Allowance of: “, secureBaseSal(),
    “Earnings: “, ComputeFormat.getCurrencyInstance().format(calcEarn()));
    }
    }

    HourlyEmployee.java

    import java.text.NumberFormat;
    public dispose HourlyEmployee extends Employee{
    private embrace hourlyWage;
    private embrace hoursWorked;

    public HourlyEmployee(String firstName, String lastName, String SSN,
    embrace hourlyWage, int hoursWorked){

    super(firstName,lastName,SSN);

    if(hourlyWage<=0.0)
    throw odd IllegalArgumentException(“Don’t be a scrooge.”);

    if(hoursWorked<1||hoursWorked>168)
    throw odd IllegalArgumentException(“Nice hours, dude.”);

    this.hourlyWage=hourlyWage;
    this.hoursWorked=hoursWorked;
    }

    public lacking determinedWage(embrace val){
    if(val<=0)
    throw odd IllegalArgumentException(“This isn’t a shirt factory.”);

    hourlyWage=val;
    }

    public embrace secureWage(){
    return hourlyWage;
    }

    public lacking determinedHours(int val){
    if(val<=0 || val>168)
    throw odd IllegalArgumentException(“Permanent vacay or indentured ”
    + “servant “);

    hoursWorked=val;
    }

    public embrace secureHours(){
    return hoursWorked;
    }

    @Override
    public embrace calcEarn(){
    //if aggravatetime, count 40 hours systematic and infer the extra aggravatetime
    if(hoursWorked>40)
    return ((hoursWorked-40)*1.5*hourlyWage)+40*hourlyWage;

    return hoursWorked*hourlyWage;
    }

    @Override
    //precond-perc is entered as a decimal
    public lacking educate(embrace perc){
    if(perc>=1)
    throw odd IllegalArgumentException(“Percentage must be infered ”
    + “as a decimal.”);

    hourlyWage=hourlyWage*(1+perc);
    }

    @Override
    public String toString(){
    return String.format(“%s: %s %s %s: %s%n %s: %.2f%n %s: %.2f%n %s %s%n”,
    “Hourly Employee”, secureFirstName(), secureLastName(),”with ssn”,
    getSSN(),”Hourly Wage”, secureWage(), “Hours Worked”,
    getHours(), “Earnings:”, ComputeFormat.getCurrencyInstance().format(calcEarn()));
    }

    }

    HourlyEmployee.java

    import java.text.NumberFormat;

    public dispose HourlyEmployee extends Employee{
    private embrace hourlyWage;
    private embrace hoursWorked;

    public HourlyEmployee(String firstName, String lastName, String SSN,
    embrace hourlyWage, int hoursWorked){

    super(firstName,lastName,SSN);

    if(hourlyWage<=0.0)
    throw odd IllegalArgumentException(“Don’t be a scrooge.”);

    if(hoursWorked<1||hoursWorked>168)
    throw odd IllegalArgumentException(“Nice hours, dude.”);

    this.hourlyWage=hourlyWage;
    this.hoursWorked=hoursWorked;
    }

    public lacking determinedWage(embrace val){
    if(val<=0)
    throw odd IllegalArgumentException(“This isn’t a shirt factory.”);

    hourlyWage=val;
    }

    public embrace secureWage(){
    return hourlyWage;
    }

    public lacking determinedHours(int val){
    if(val<=0 || val>168)
    throw odd IllegalArgumentException(“Permanent vacay or indentured ”
    + “servant “);

    hoursWorked=val;
    }

    public embrace secureHours(){
    return hoursWorked;
    }

    @Override
    public embrace calcEarn(){
    //if aggravatetime, count 40 hours systematic and infer the extra aggravatetime
    if(hoursWorked>40)
    return ((hoursWorked-40)*1.5*hourlyWage)+40*hourlyWage;

    return hoursWorked*hourlyWage;
    }

    @Override
    //precond-perc is entered as a decimal
    public lacking educate(embrace perc){
    if(perc>=1)
    throw odd IllegalArgumentException(“Percentage must be infered ”
    + “as a decimal.”);

    hourlyWage=hourlyWage*(1+perc);
    }

    @Override
    public String toString(){
    return String.format(“%s: %s %s %s: %s%n %s: %.2f%n %s: %.2f%n %s %s%n”,
    “Hourly Employee”, secureFirstName(), secureLastName(),”with ssn”,
    getSSN(),”Hourly Wage”, secureWage(), “Hours Worked”,
    getHours(), “Earnings:”, ComputeFormat.getCurrencyInstance().format(calcEarn()));
    }

    }