Results 1 to 2 of 2
- 03-21-2011, 01:45 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Change program written with inheritance to composition
The assignment is written as:
Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlussCommissionEmployee (Fig.9.11) of the CommissionEmployee-BasePlusCommissionEmployee hierarchy to use composition rather than inheritance.
Here is what fig.9.11 looks like:
I'm gonna be honest..I've read the chapter twice and I still have no idea where to even start. Can someone help me understand this a little better.Java Code:public class BasePlusCommissionEmployee extends 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 ) { super( first, last, ssn, sales, rate ); setBaseSalary( salary ); // validate and store base salary } // end six-argument BasePlusCommissionEmployee constructor // set base salary public void setBaseSalary( double salary ) { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; } // end method setBaseSalary // return base salary public double getBaseSalary() { return baseSalary; } // end method getBaseSalary // calculate earnings @Override // indicates that this method overrides a superclass method public double earnings() { return getBaseSalary() + super.earnings(); } // end method earnings // return String representation of BasePlusCommissionEmployee @Override // indicates that this method overrides a superclass method public String toString() { return String.format( "%s %s\n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary() ); } // end method toString } // end class BasePlusCommissionEmployee
- 03-21-2011, 02:14 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
This is what i've come up with. Am I even in the same ball park here?
Java Code:public class BasePlusCommissionEmployee { private CommissionEmployee commissionEmployee; // composition private double baseSalary; // base salary per week // six-argument constructor public BasePlusCommissionEmployee( String first, String last, String ssn, double sales, double rate, double salary ) { 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 ) { commissionEmployee.setFirstName( first ); } // end method setFirstName // return first name public String getFirstName() { return commissionEmployee.getFirstName(); } // end method getFirstName // set last name public void setLastName( String last ) { commissionEmployee.setLastName( last ); } // end method setLastName // return last name public String getLastName() { return commissionEmployee.getLastName(); } // end method getLastName // set social security number public void setSocialSecurityNumber( String ssn ) { commissionEmployee.setSocialSecurityNumber( ssn ); } // end method setSocialSecurityNumber // return social security number public String getSocialSecurityNumber() { return commissionEmployee.getSocialSecurityNumber(); } // end method getSocialSecurityNumber // set commission employee's gross sales amount public void setGrossSales( double sales ) { commissionEmployee.setGrossSales( sales ); } // end method setGrossSales // return commission employee's gross sales amount public double getGrossSales() { return commissionEmployee.getGrossSales(); } // end method getGrossSales // set commission employee's rate public void setCommissionRate( double rate ) { commissionEmployee.setCommissionRate( rate ); } // end method setCommissionRate // return commission employee's rate public double getCommissionRate() { return 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() { return getBaseSalary() + commissionEmployee.earnings(); } // end method earnings // return String representation of BasePlusCommissionEmployee @Override public String toString() { return String.format( "%s %s\n%s: %.2f", "base-salaried", commissionEmployee.toString(), "base salary", getBaseSalary() ); } // end method toString } // end class BasePlusCommissionEmployee
I get the following errors when I try to compile it though:
Java Code:BasePlusCommissionEmployee.java:3: cannot find symbol symbol : class CommissionEmployee location: class BasePlusCommissionEmployee private CommissionEmployee commissionEmployee; // composition ^ BasePlusCommissionEmployee.java:11: cannot find symbol symbol : class CommissionEmployee location: class BasePlusCommissionEmployee new CommissionEmployee( first, last, ssn, sales, rate ); ^ BasePlusCommissionEmployee.java:90: operator + cannot be applied to double,CommissionEmploy ee.earnings return getBaseSalary() + commissionEmployee.earnings(); ^ BasePlusCommissionEmployee.java:90: incompatible types found : <nulltype> required: double return getBaseSalary() + commissionEmployee.earnings();
Similar Threads
-
GUI AND INHERITANCE program help
By sssss in forum New To JavaReplies: 1Last Post: 01-19-2011, 06:57 PM -
Composition vs inheritance
By mindus in forum Advanced JavaReplies: 3Last Post: 09-27-2010, 07:41 AM -
composition Vs Inheritance
By mindus in forum New To JavaReplies: 1Last Post: 09-26-2010, 07:53 PM -
How is Java inheritance being used to enforce program requirements?
By Greatwolf in forum New To JavaReplies: 4Last Post: 07-22-2010, 11:22 AM -
Inheritance Program
By Schaput in forum New To JavaReplies: 6Last Post: 11-14-2008, 12:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks