Exercises 9.3 Many program written with inheritance could be written with compos
ID: 3672414 • Letter: E
Question
Exercises 9.3 Many program written with inheritance could be written with composition instead and vice versa. Rewrite class BasePlusCommissionEmployee (fig9.11 ) of the commissionEmployeeBasePlusCommissionEmployee hierarchy to use composition rather than inheritance. Please test your answers to insure no errors before posting. Fi
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
//
Explanation / Answer
public class BasePlusCommissionEmployee
{
/* declare instance variable to satisfy the has-a relationship */
CommissionEmployee commissionEmployee;
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 */
commissionEmployee = new CommissionEmployee(first, last, ssn, sales, rate);
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 */
commissionEmployee.setFirstName(first);
} // end method setFirstName
// return first name
public String getFirstName()
{
/* return the first name of the composed CommissionEmployee object */
commissionEmployee.getFirstName();
} // end method getFirstName
// set last name
public void setLastName( String last )
{
/* set the last name of the composed CommissionEmployee object */
commissionEmployee.setLastName(last);
} // end method setLastName
// return last name
public String getLastName()
{
/* return the last name of the composed CommissionEmployee object */
commissionEmployee.getLastName();
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
/* set the social security number of the composed CommissionEmployee object */
commissionEmployee.setSocialSecurityNumber(ssn );
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
/* return the social security number of the composed CommissionEmployee
object */
commissionEmployee.getSocialSecurityNumber();
} // end method getSocialSecurityNumber
// set commission employee's gross sales amount
public void setGrossSales( double sales )
{
/* set the gross sales of the composed CommissionEmployee object */
commissionEmployee.setGrossSales(sales );
} // end method setGrossSales
// return commission employee's gross sales amount
public double getGrossSales()
{
/* return the gross sales of the composed CommissionEmployee object */
commissionEmployee.getGrossSales();
} // end method getGrossSales
// set commission employee's rate
public void setCommissionRate( double rate )
{
/* Set the commission rate of the composed CommissionEmployee object */
commissionEmployee.setCommissionRate(rate )
} // end method setCommissionRate
// return commission employee's rate
public double getCommissionRate()
{
/* Return the commission rate of the composed CommissionEmployee object */
commissionEmployee.getCommissionRate();
} // 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 */
return commissionEmployee.getCommissionRate()*commissionEmployee.getGrossSales();
} // 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 */
return "["+commissionEmployee.getFirstName()+", "+commissionEmployee.getLastName()+
", "+commissionEmployee.getSocialSecurityNumber()+", "+commissionEmployee.getCommissionRate()+
", "+ commissionEmployee.getGrossSales()+", "+getBaseSalary()+"]"
} // end method toString
} // end class BasePlusCommissionEmployee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.