Question
JAVA Tasks : Combining Composition and Inheritance In Chapter 10, we discussed instead of using an inheritance hierarchy, we can declare the class Employee in
JAVA Tasks:Combining Composition and Inheritance
In Chapter 10, we discussed instead of using an inheritance hierarchy, we can declare the class Employee in which each Employee has a different CompensationModel object. To achieve that we need four different instance variable instead of three. The fourth variable would be of type CompensationModel. 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 instance variables.
A set method that allows the client code to change an Employees CompensationModel.
An earnings method that calls the CompensationModels earnings method and returns its result.
Now, implement CompensationModel as an interface that provides a public abstract method earnings that receives no parameters and returns a double. Then create the following classes that implement interface CompensationModel:
SalariedCompensationModelFor Employees who are paid a fixed weekly salary, this class should contain a weeklySalary instance variable, and should implement method earnings to return the weeklySalary.
HourlyCompensationModelFor Employees who are paid by the hour and receive overtime pay for all hours worked in excess of 40 hours per week, this class should contain wageand hours instance variables, and should implement method earnings based on the number of hours worked (see class HourlyEmployees earnings method in Fig.10.6 ).
CommissionCompensationModelFor Employees who are paid by commission, this class should contain grossSales and commissionRate instance variables, and should implement method earnings to return grossSales * commissionRate.
BasePlusCommissionCompensationModelFor Employees who are paid a base salary and commission, this class should contain instance variables grossSales, commission-Rate and baseSalary and should implement earnings to return baseSalary + gross-Sales * commissionRate.
Use the following test application: EmployeeTest.java Download EmployeeTest.java
Make sure that you are getting the following output:
Name:: John Smith SSN: 111-11-1111 Compensation Model: Salaried Weekly Salary: 800.00 Earnings: 800.0
Name:: Karen Price SSN: 222-22-2222 Compensation Model: Hourly Wage: 16.75 Hours: 40.00 Earnings: 670.0
Name:: John Smith SSN: 111-11-1111 Compensation Model: Commission Gross Sales: 10000.00 Comission Rate: 0.06 Earnings: 600.0
Name:: Karen Price SSN: 222-22-2222 Compensation Model: Base Plus Commission Gross Sales: 5000.00 Comission Rate: 0.04 Base Salary: 300.00 Earnings: 500.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started