Question: Java How To Program (Early Objects)(10th Edition) Chapter 9 Exercise 9.3 ( Using Composition Rather Than Inheritance ) Many programs written with inheritance could be

Java How To Program (Early Objects)(10th Edition) Chapter 9

Exercise 9.3 (Using Composition Rather Than Inheritance) Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee (Figure 9.11) of the commissionEmployee-BasePlusCommissionEmployee hierarchy to use composition rather than inheritance.

Figure 9.11

// Exercise 9.3 solution: BasePlusCommissionEmployee.java

// BasePlusCommissionEmployee using composition.

public class BasePlusCommissionEmployee

{

/* declare instance variable to satisfy the has-a relationship */

private double baseSalary; // base salary per week

// six-argument constructor

public BasePlusCommissionEmployee( String first, String last,

String ssn, double sales, double rate, double salary )

{

/* construct the CommissionEmployee portion of this object */

setBaseSalary( salary ); // validate and store base salary

} // end six-argument BasePlusCommissionEmployee constructor

// set first name

public void setFirstName( String first )

{

/* set the first name of the composed CommissionEmployee object */

} // end method setFirstName

// return first name

public String getFirstName()

{

/* return the first name of the composed CommissionEmployee object */

} // end method getFirstName

// set last name

public void setLastName( String last )

{

/* set the last name of the composed CommissionEmployee object */

} // end method setLastName

// return last name

public String getLastName()

{

/* return the last name of the composed CommissionEmployee object */

} // end method getLastName

// set social security number

public void setSocialSecurityNumber( String ssn )

{

/* set the social security number of the composed CommissionEmployee object */

} // end method setSocialSecurityNumber

// return social security number

public String getSocialSecurityNumber()

{

/* return the social security number of the composed CommissionEmployee

object */

} // end method getSocialSecurityNumber

// set commission employee's gross sales amount

public void setGrossSales( double sales )

{

/* set the gross sales of the composed CommissionEmployee object */

} // end method setGrossSales

// return commission employee's gross sales amount

public double getGrossSales()

{

/* return the gross sales of the composed CommissionEmployee object */

} // end method getGrossSales

// set commission employee's rate

public void setCommissionRate( double rate )

{

/* Set the commission rate of the composed CommissionEmployee object */

} // end method setCommissionRate

// return commission employee's rate

public double getCommissionRate()

{

/* Return the commission rate of the composed CommissionEmployee object */

} // end method getCommissionRate

// set base-salaried commission employee's base salary

public void setBaseSalary( double salary )

{

baseSalary = ( salary < 0.0 ) ? 0.0 : salary;

} // end method setBaseSalary

// return base-salaried commission employee's base salary

public double getBaseSalary()

{

return baseSalary;

} // end method getBaseSalary

// calculate base-salaried commission employee's earnings

public double earnings()

{

/* Calculate the earnings of this object using the earnings of the composed

CommissionEmployee object */

} // end method earnings

// return String representation of BasePlusCommissionEmployee

public String toString()

{

/* Return a string consisting of the string representation of the composed

CommissionEmployee object along with the base salary */

} // end method toString

} // end class BasePlusCommissionEmployee

//

IMPORTANT NOTE!!: Please add detailed, but brief comments to most, if not all of the code to explain what is being used in the code and what is does so I have a better understanding of the code. Also I've tried previously already posted answers to this question but they had errors and I couldn't figure out why. Thank you.

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!