Develop a Java program that uses: Inheritance constructs overriding superclass m
ID: 673000 • Letter: D
Question
Develop a Java program that uses:
Inheritance constructs
overriding superclass methods
invoking super class methods
accessing superclass instance variables
abstact classes and methods
Polymorphism
Container operations, access all elements
Prep Readings:
Absolute Java, Chapters 1 - 9.
Project Requirements:
We are going to develop a program that implements inheritance to process employees in an organization. The program will consist of the following classes: Employee, HourlyEmployee, SalariedEmployee, CommissionedEmployee and Company and CompanyTester.
The Employee Class
This is an Abstract class designed to represent an Employee in a company.
Instance Variables.
Employees Name
Employee id number
Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Employee's IDNum
Methods
Constructor - sets all instance fields from parameters.
Constructor - default constructor, sets all instance field to a default value.
Access and mutator methods for all variables. NOTE Mutator method for IDNum should use the static variable nextIDNum.
computePay - Abstract method that computes employee pay.
toString method - returns a neatly formatted string containing the s name and id number
The HourlyEmployee Class
Subclass of Employee Class
Instance Variable
employees hourly rate of pay
hours worked during current week
Methods
Constructor - sets all instance fields from parameters.
Default Constructor - sets all instance fields to a default value.
Access and mutator methods for instance variable.
Overwritten computePay method
1. pay calculation
1. <= 40 hours - rate * hours
2. > 40 hours - rate * 40 + (hours above 40 * rate * 1.5).
toSting - toString method - returns a neatly formatted string containing the employees name, id number (where have you see this before) and hourly rate of pay.
The SalariedEmployee Class
Subclass of Employee Class
Instance Variable - weekly salary
Methods
Constructor - sets all instance fields from parameters.
Default Constructor - sets all instance fields to a default value.
Accessor and mutator methods for instance field.
Overwritten computePay method - Returns weekly salary
toString method - returns a neatly formatted string containing the employees name, id number and weekly salary
The CommissionedEmployee Class
Subclass of SalariedEmployee
Instance Variable
weeklysales
commission percentage
Methods
Constructor - sets all instance fields from parameters.
Default Constructor - sets all instance fields to a default value.
Accessor and mutator methods for instance fields.
Overwritten computePay method
pay calculation
add commission to weekly salary
commission = weeklysales * commission percentage.
toString method - returns a neatly formatted string containing the employees name, id number, weekly salary and commission
The Company Class
Holds and manages all the employee objects.
Instance fields
Name of company
A container (ArrayList or Array) to hold the abstract classes employee objects. ArrayList are not covered until Chapter 14 but they are fairly simple to use if you’d like to use them in this project.
Methods
Constructor - sets the instance field from parameter and creates the container.
Default Constructor - sets the instance field to a default value and creates the container
Add an employee to the container
Get an employee by employee Id
Get the number of employees in the company
Get the total payroll for the company assuming all employees work a 40 hour work week.
toString method that returns a neatly formatted string the company name and each employees information (employees name, id number and weekly pay (assume 40 hour work week).
CompanyTester Class
Creates a Company Object
Adds several employees of each type to the Company class.
Tests all the methods in all the classes, either directly or indirectly.
Create 2 UML Class Diagram for this project.
Design Version - completed prior to coding the project to assist with coding.
Final Version - completed after the coding that accurately represents the final version of the code.
All instance variables, including type.
All methods, including parameter list, return type and access specifier (+, -);
No need to include the CompanyTester in the UML diagrams. Refer to the UML Distilled pdf on the content page as a referene for creating class diagrams.
Note: Some design details such as instance variable types and method names and parameters have not been specified, this is intentional, use your experience and best judgment to fill in the details, just make sure you meet the requirements.
Explanation / Answer
// Employee Class
package employee;
//abstract class
public abstract class Employee {
String Name;
int IDnum;
static int nextIDNum;
//Constructor - default constructor, sets all instance field to a default value.
public Employee()
{
Name="";
IDnum=0;
nextIDNum=1000;
}
//Constructor - sets all instance fields from parameters.
public Employee(String n,int id )
{
Name=n;
IDnum=id;
nextIDNum=1000;
}
//Access and mutator methods for all variables.
//NOTE Mutator method for IDNum should use the static variable nextIDNum.
public void setName(String n)
{
this.Name=n;
}
public String getName()
{
return this.Name;
}
public void setIDnum()
{
this.IDnum=nextIDNum++;
}
public int getIDnum()
{
return this.IDnum;
}
//computePay - Abstract method that computes employee pay.
public abstract double computePay();
//toString method - returns a neatly formatted string containing the s name and id number
public String toString()
{
String res="Employee Name: "+Name+" Employee IDnumber: "+IDnum;
return res;
}
}
--------------------------------------------------------------------------------------------------------------
package employee;
// subclass of employee class
public class HourlyEmployee extends Employee{
double hourlyRateOfPay;
int hoursWorked;// during current week
//Constructor - default constructor, sets all instance field to a default value.
public HourlyEmployee()
{
super("",0);
hourlyRateOfPay=0;
hoursWorked=0;
}
//Constructor - sets all instance fields from parameters.
public HourlyEmployee(String n,int id,int hours )
{
super(n,id);
hoursWorked=hours;
}
//Access and mutator methods for all variables.
//NOTE Mutator method for IDNum should use the static variable nextIDNum.
public void setHourlyRateOfPay(double Pay)
{
hourlyRateOfPay=Pay;
}
public double getHourlyRateOfPay()
{
return hourlyRateOfPay;
}
public void setHoursWorked(int hours)
{
hoursWorked=hours;
}
public int getHoursWorked()
{
return hoursWorked;
}
@Override
public double computePay() {
double rate=10;
// Overwritten computePay method
if(hoursWorked<=40)
hourlyRateOfPay=hoursWorked - rate * hoursWorked;
else if(hoursWorked>40)
hourlyRateOfPay=hoursWorked - rate * 40 + ((40-hoursWorked) * rate * 1.5);
return hourlyRateOfPay;
}
public String toString()
{
return ("Employee Name: "+Name+" Employee IDnumber: "+IDnum+" Pay:"+hourlyRateOfPay);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.