Question: Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlus-CommissionEmployee (Fig. 9.11) of the CommissionEmployeeBasePlusCommissionEmployee hierarchy so that
Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlus-CommissionEmployee (Fig. 9.11) of the CommissionEmployee–BasePlusCommissionEmployee hierarchy so that it contains a reference to a CommissionEmployee object, rather than inheriting from class CommissionEmployee. Retest BasePlusCommissionEmployee to demonstrate that it still provides the same functionality.
Fig. 9.11
1 2 3 4 10 II 12 5 public class BasePlusCommission Employee extends Commission Employee { private double baseSalary; // base salary per week 6 7 8 13 14 15 16 17 18 Imm 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 44 42 45 44 45 41 // Fig. 9.11: BasePlusCommission Employee.java // BasePlusCommission Employee class inherits from Commission Employee // and accesses the superclass's private data via inherited // public methods. 43 } // six-argument constructor public BasePlusCommission Employee (String firstName, String lastName, String social SecurityNumber, double grossSales, double commission Rate, double baseSalary) { super(firstName, lastName, social SecurityNumber, grossSales, commissionRate); // if baseSalary is invalid throw exception if (baseSalary < 0.0) { throw new Illegal ArgumentException ("Base salary must be >= 0.0"); } this.baseSalary = baseSalary; } // set base salary public void setBase Salary(double baseSalary) { if (baseSalary < 0.0) { throw new IllegalArgumentException ("Base salary must be >= 0.0"); } this.baseSalary = baseSalary; } // return base salary public double getBase Salary() {return baseSalary;} // calculate earnings @Override public double earnings() {return getBaseSalary() + super. earnings (); } } // return String representation of BasePlusCommission Employee @Override public String toString() { return String.format("%s %s %n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary());
Step by Step Solution
3.35 Rating (158 Votes )
There are 3 Steps involved in it
The provided code shows a BasePlusCommissionEmployee class ... View full answer
Get step-by-step solutions from verified subject matter experts
