Question
9.14 ( Employee Hierarchy) In this chapter, you studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of
9.14 (Employee Hierarchy) In this chapter, you studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, youll create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, socialSecurityNumber, getFirstName, getLastName, getSocialSecurityNumber and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. Class CommissionEmployees constructor should invoke class Employees constructor and CommissionEmployees toString method should invoke Employees toString method. Once youve completed these modifications, run the CommissionEmployeeTest and BasePlusCommissionEmployeeTest apps using these new classes to ensure that the apps still display the same results for a CommissionEmployee object and BasePlusCommissionEmployee object, respectively.
public class CommissionEmployee 5 { 6 private final String firstName; 7 private final String lastName; 8 private final String socialSecurityNumber; 9 private double grossSales; // gross weekly sales 10 private double commissionRate; // commission percentage 11 12 // five-argument constructor 13 public CommissionEmployee(String firstName, String lastName, 14 String socialSecurityNumber, double grossSales, 15 double commissionRate) 16 { 17 // implicit call to Object constructor occurs here 18 19 // if grossSales is invalid throw exception 20 if (grossSales < 0.0) 21 throw new IllegalArgumentException( 22 "Gross sales must be >= 0.0"); 23 24 // if commissionRate is invalid throw exception 25 if (commissionRate <= 0.0 || commissionRate >= 1.0) 26 throw new IllegalArgumentException( 27 "Commission rate must be > 0.0 and < 1.0"); 28 29 this.firstName = firstName; 30 this.lastName = lastName; 31 this.socialSecurityNumber = socialSecurityNumber; 32 this.grossSales = grossSales; 33 this.commissionRate = commissionRate; 34 } // end constructor 35 36 // return first name 37 public String getFirstName() 38 { 39 return firstName; 40 } 41 42 // return last name 43 public String getLastName() 44 { 45 return lastName; 46 } 47 48 // return social security number 49 public String getSocialSecurityNumber() 50 { 51 return socialSecurityNumber; 52 } 53 54 // set gross sales amount 55 public void setGrossSales(double grossSales) 56 { 57 if (grossSales < 0.0) 58 throw new IllegalArgumentException( 59 "Gross sales must be >= 0.0"); 60 61 this.grossSales = grossSales; 62 } 63 64 // return gross sales amount 65 public double getGrossSales() 66 { 67 return grossSales; 68 } 69 70 // set commission rate 71 public void setCommissionRate(double commissionRate) 72 { 73 if (commissionRate <= 0.0 || commissionRate >= 1.0) 74 throw new IllegalArgumentException( 75 "Commission rate must be > 0.0 and < 1.0"); 76 77 this.commissionRate = commissionRate; 78 } 79 80 // return commission rate 81 public double getCommissionRate() 82 { 83 return commissionRate; 84 } 85 86 // calculate earnings 87 public double earnings() 88 { 89 return getCommissionRate() * getGrossSales(); 90 } 91 92 // return String representation of CommissionEmployee object 93 @Override 94 public String toString() 95 { 96 return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", 97 "commission employee", getFirstName(), getLastName(), 98 "social security number", getSocialSecurityNumber(), 99 "gross sales", getGrossSales(), 100 "commission rate", getCommissionRate()); 101 } 102 } // end class CommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee 7 { 8 private double baseSalary; // base salary per week 9 10 // six-argument constructor 11 public BasePlusCommissionEmployee(String firstName, String lastName, 12 String socialSecurityNumber, double grossSales, 13 double commissionRate, double baseSalary) 14 { 15 super(firstName, lastName, socialSecurityNumber, 16 grossSales, commissionRate); 17 18 // if baseSalary is invalid throw exception 19 if (baseSalary < 0.0) 20 throw new IllegalArgumentException( 21 "Base salary must be >= 0.0"); 22 23 this.baseSalary = baseSalary; 24 } 25 26 // set base salary 27 public void setBaseSalary(double baseSalary) 28 { 29 if (baseSalary < 0.0) 30 throw new IllegalArgumentException( 31 "Base salary must be >= 0.0"); 32 33 this.baseSalary = baseSalary; 34 } 35 36 // return base salary 37 public double getBaseSalary() 38 { 39 return baseSalary; 40 } 41 42 // calculate earnings 43 @Override 44 public double earnings() 45 { 46 return getBaseSalary() + super.earnings(); 47 } 48 49 // return String representation of BasePlusCommissionEmployee 50 @Override 51 public String toString() 52 { 53 return String.format("%s %s%n%s: %.2f", "base-salaried", 54 super.toString(), "base salary", getBaseSalary()); 55 } 56 } // end class BasePlusCommissionEmployee
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started