Question: ======================================================================================== Commission Employee Class ========================================================================================= public class CommissionEmployee { private final String firstName; private final String lastName; private final String socialSecurityNumber; private double grossSales; //

 ======================================================================================== Commission Employee Class ========================================================================================= public class CommissionEmployee { private final

========================================================================================

Commission Employee Class

=========================================================================================

public class CommissionEmployee { private final String firstName; private final String lastName; private final String socialSecurityNumber; private double grossSales; // gross weekly sales private double commissionRate; // commission percentage

// five-argument constructor public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) { // implicit call to Object constructor occurs here

// if grossSales is invalid throw exception if (grossSales = 0.0"); }

// if commissionRate is invalid throw exception if (commissionRate = 1.0) { throw new IllegalArgumentException( "Commission rate must be > 0.0 and

this.firstName = firstName; this.lastName = lastName; this.socialSecurityNumber = socialSecurityNumber; this.grossSales = grossSales; this.commissionRate = commissionRate; }

// return first name public String getFirstName() {return firstName;}

// return last name public String getLastName() {return lastName;}

// return social security number public String getSocialSecurityNumber() {return socialSecurityNumber;}

// set gross sales amount public void setGrossSales(double grossSales) { if (grossSales = 0.0"); }

this.grossSales = grossSales; }

// return gross sales amount public double getGrossSales() {return grossSales;}

// set commission rate public void setCommissionRate(double commissionRate) { if (commissionRate = 1.0) { throw new IllegalArgumentException( "Commission rate must be > 0.0 and

this.commissionRate = commissionRate; }

// return commission rate public double getCommissionRate() {return commissionRate;}

// calculate earnings public double earnings() { return getCommissionRate() * getGrossSales(); }

// return String representation of CommissionEmployee object @Override public String toString() { return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", "commission employee", getFirstName(), getLastName(), "social security number", getSocialSecurityNumber(), "gross sales", getGrossSales(), "commission rate", getCommissionRate()); } }

=========================================================================================

BasePlusCommissionEmployee

=========================================================================================

public class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; // base salary per week

// six-argument constructor public BasePlusCommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary) { super(firstName, lastName, socialSecurityNumber, grossSales, commissionRate);

// if baseSalary is invalid throw exception if (baseSalary = 0.0"); }

this.baseSalary = baseSalary; } // set base salary public void setBaseSalary(double baseSalary) { if (baseSalary = 0.0"); }

this.baseSalary = baseSalary; }

// return base salary public double getBaseSalary() {return baseSalary;}

// calculate earnings @Override public double earnings() {return getBaseSalary() + super.earnings();}

// return String representation of BasePlusCommissionEmployee @Override public String toString() { return String.format("%s %s%n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary()); } }

=========================================================================================

EmployeeTest

=========================================================================================

public class BasePlusCommissionEmployeeTest { public static void main(String[] args) { // instantiate BasePlusCommissionEmployee object BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee( "Bob", "Lewis", "333-33-3333", 5000, .04, 300); // get base-salaried commission employee data System.out.println( "Employee information obtained by get methods:%n"); System.out.printf("%s %s%n", "First name is", employee.getFirstName()); System.out.printf("%s %s%n", "Last name is", employee.getLastName()); System.out.printf("%s %s%n", "Social security number is", employee.getSocialSecurityNumber()); System.out.printf("%s %.2f%n", "Gross sales is", employee.getGrossSales()); System.out.printf("%s %.2f%n", "Commission rate is", employee.getCommissionRate()); System.out.printf("%s %.2f%n", "Base salary is", employee.getBaseSalary());

employee.setBaseSalary(1000); System.out.printf("%n%s:%n%n%s%n", "Updated employee information obtained by toString", employee.toString()); } }

9.16 (Recommended Projecr: Combining Composition and Inheritance) In this chapter, we cre ated the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the re lationship between two cypes of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Em ployees and that each has a different CompensationModel object A CompensationModel would provide an earnings method. Subclasses of CompensationModel would contain the details of a particular Employee's compensation: a) CommissionCompensationModel-For Employees who are paid by commission, this CompensationModel subclass would contain grossSales and commissionRate instance variables, and would define earnings to return grossSals commissionRate b) BasePlusCommissionCompensationMode1-For Employees who are paid a hase salary and commission, this CompensationModel subclass would contain instance variables grossSales, commissionRate and basesalary and would define earnings to return baseSalary grossSales commissionRate. Class Employee's earnings method would simply call the composed CompensationModel's earnings method and return its result This approach is more flexible than our original hierarchy. For example, consider an Employee who gets promoted. With the approach described here, you can simply change that Employe CompensationModel by assigning the composed CompensationModel reference an appropriate sub- class object. With the CommissionEmployee-BasePlusCommissionEmployee hierarchy, youd need to change the employee's type by creating a new object of the appropriate class and moving data from the old object into the new one. Implement the Employee class and CompensationMode1 hierarchy discussed in this exercise. In addition to the firstName, lastName, socialSecurityNumber and CompensationModel instance variables, class Employee should provide: A constructor that receives three Strings and a CompensationModel to initialize the in stancc variables b) A set method that allows the client code to change an Employee's CompensationModel. An earnings method that calls the CompensationModel's earnings method and returns is resulr When you invoke method earnings via the superclass CompensationModel reference to a subclass object (of type CommissionCompensationModel or BasePlusCommissionCompensationModel), you might expect superclass CompensationModel's earnings method to execute. What actually hap- ns? The subclass object's earnings method executes. This is called polymorphic behavior, which we explore in Chapter 10 In your test application, create two Employee objects-one with a CommissionCompensation- Model and one with a BasePlusCommissionCompensationModethen display each Employee's earn- ings. Next, change each Employee's CompensationModel dynamically and redisplay each Employee's earnings. In Chapter 10's exercises, we'll examine how to implement CompensationModel as an interface rather than a class. 9.16 (Recommended Projecr: Combining Composition and Inheritance) In this chapter, we cre ated the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the re lationship between two cypes of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Em ployees and that each has a different CompensationModel object A CompensationModel would provide an earnings method. Subclasses of CompensationModel would contain the details of a particular Employee's compensation: a) CommissionCompensationModel-For Employees who are paid by commission, this CompensationModel subclass would contain grossSales and commissionRate instance variables, and would define earnings to return grossSals commissionRate b) BasePlusCommissionCompensationMode1-For Employees who are paid a hase salary and commission, this CompensationModel subclass would contain instance variables grossSales, commissionRate and basesalary and would define earnings to return baseSalary grossSales commissionRate. Class Employee's earnings method would simply call the composed CompensationModel's earnings method and return its result This approach is more flexible than our original hierarchy. For example, consider an Employee who gets promoted. With the approach described here, you can simply change that Employe CompensationModel by assigning the composed CompensationModel reference an appropriate sub- class object. With the CommissionEmployee-BasePlusCommissionEmployee hierarchy, youd need to change the employee's type by creating a new object of the appropriate class and moving data from the old object into the new one. Implement the Employee class and CompensationMode1 hierarchy discussed in this exercise. In addition to the firstName, lastName, socialSecurityNumber and CompensationModel instance variables, class Employee should provide: A constructor that receives three Strings and a CompensationModel to initialize the in stancc variables b) A set method that allows the client code to change an Employee's CompensationModel. An earnings method that calls the CompensationModel's earnings method and returns is resulr When you invoke method earnings via the superclass CompensationModel reference to a subclass object (of type CommissionCompensationModel or BasePlusCommissionCompensationModel), you might expect superclass CompensationModel's earnings method to execute. What actually hap- ns? The subclass object's earnings method executes. This is called polymorphic behavior, which we explore in Chapter 10 In your test application, create two Employee objects-one with a CommissionCompensation- Model and one with a BasePlusCommissionCompensationModethen display each Employee's earn- ings. Next, change each Employee's CompensationModel dynamically and redisplay each Employee's earnings. In Chapter 10's exercises, we'll examine how to implement CompensationModel as an interface rather than a class

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 Databases Questions!