Objectives The focus of this assignment is the use of inheri
Objectives: The focus of this assignment is the use of inheritance. You will need to understand and implement the ideas of basic inheritance.
Program Description:
This project will have you create a simple class hierarchy building upon the Employee example discussed in class.
A total of five classes are required.
Employee – A class which the other employee types inherit from
HourlyEmployee – An employee whose pay is based upon an hourly wage and hours worked
SalaryEmployee – An employee whose pay is based upon a yearly salary
CommissionEmployee – An employee whose pay is based upon a commission rate and sales amount
EmployeeTest – Contains main method. Creates one of each sub type of Employee and performs actions upon them. This is provided to you. DO NOT MAKE CHANGES
UML DIAGRAM FOR AND DISCUSSION FOR Employee
Employee
- String firstName
- String lastName
- char middleInitial
- boolean fulltime
- char gender
- int employeeNum
<<constructor>> Employee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean )
+ getEmployeeNumber() : int
+ setEmployeeNumber(empNum : int)
+ getFirstName() : String
+ getLastName() : String
+ getMiddleInitial() : char
+ getGender() : char
+ setFirstName(fn: String)
+ setLastName(ln : String)
+ setMiddleI(m : char)
+ setGender(g : char)
+ equals(e2 : Object) : boolean
+ toString() : String
Notes on Data Members
Your constructor should call upon the set methods for the data members it is initializing. This is especially important for employeeNum and gender since there is validation required.
The employeeNum member must be between 10000 and 99999, inclusive. If an invalid value is passed, the Employee class should immediately ask for another number until an acceptable one is given. This should be handled in setEmployeeNumber.
If an invalid value for gender is given (not ‘M’ or ‘F’, specifically uppercase) it should default to ‘F’. This should be handled in setGender.
Notes on Methods
equals() – Overrides Object equals(). Returns true if the employeeNum of the two instances are equal, false otherwise.
toString() – Overrides Object toString(). Returns as String of the Employee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
(Note that Status doesn’t say true or false, rather Full Time or Part Time)
DISSCUSSION ON Employee SUBCLASSES
HourlyEmployee
HourlyEmployee extends Employee
- double wage
- double hoursWorked
<<constructor>> HourlyEmployee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean, w : double )
+ increaseHours(hours : double)
+ toString() : String
+ calculateWeeklyPay() : double
+ annualRaise()
+ holidayBonus() : double
+ resetWeek()
Additional Data Members:
- double wage
- double hoursWorked
Methods
Constructor accepts all that an Employee requires as well as a double for wage, hoursWorked set at 0.0.
Override toString(), returns a String of the HourlyEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Wage: 3.40
Hours Worked: 0.00
calculateWeeklyPay() – Return amount earned in the week using wage and hoursWorked, any hours worked over 40 give double pay
annualRaise() – Wage is increased by 5%, round down to 2 decimal places
holidayBonus() – Return amount of 40 hours worked (40*wage)
resetWeek() – Resets hours worked to 0
increaseHours() - This class also needs the ability to increase the hours worked. Requesting to increase by a negative value should give no change, and report an error to the user.
SalaryEmployee
SalaryEmployee extends Employee
- salary : double
<<constructor>> SalaryEmployee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean, s : double )
+ toString() : String
+ calculateWeeklyPay() : double
+ annualRaise()
+ holidayBonus() : double
+ resetWeek()
Additional Data Members
- double salary
Methods
Constructor accepts all that an Employee requires as well as a double for salary.
Override toString(), returns a String of the SalaryEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Salary: 50000.00
calculateWeeklyPay() – Return amount earned in the week by dividing salary by 52
annualRaise() – Salary is increased by 6%, round down to 2 decimal places
holidayBonus() – Return 3% of salary
resetWeek() – No change
CommissionEmployee
CommissionEmployee extends Employee
- sales : double
- rate : double
<<constructor>> CommissionEmployee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean, r : double )
+ increaseSales(sales : double)
+ toString() : String
+ calculateWeeklyPay() : double
+ annualRaise()
+ holidayBonus() : double
+ resetWeek()
Additional Data Members
- double sales
- double rate (stored as a percent, eg. 3.5% would be stored as 3.5)
Methods
Constructor accepts all that an Employee requires as well as a double for rate, sales set to 0.0.
Override toString(), returns a String of the CommissionEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Rate: 3.50
Sales: 0.00
calculateWeeklyPay() – Return rate percentage of sales
annualRaise() – Rate percentage increased .2% example, if rate was 2.5, it becomes 2.7
holidayBonus() – No bonus
resetWeek() – Reset sales to 0.0
increaseSales() - This class also needs the ability to increase the sales. Requesting to increase by a negative value should give no change and report the error.
Package Structure:
The classes you create must follow the given package structure –
· Employee belongs to the employeeType.employee package
· HourlyEmployee, SalaryEmployee, and CommissionEmployee belong to the employeeType.subTypes package
· EmployeeTest does not declare any package
These packages must exist in your source directory. The source code (.java files) must be placed in the package directory in which they are declared to belong. Those classes that are not declared to belong to a package will exist in the default package (directly in the source directory).
This directory structure must be upheld for all future iterations of the project as well.
Other Notes:
· Make sure monetary values are formatted correctly
· Stored monetary amounts are forced to be 2 decimal places
o Keep this in mind when applying raises, for example if a wage is 9.50, increasing by 5% gives 9.975, this should be rounded DOWN to 9.97
· Make proper use of super
Summary of Files
You will need to five files for this assignment.
· Employee.java
· HourlyEmployee.java
· SalaryEmployee.java
· CommissionEmployee.java
· EmployeeTest.java (provided)
Required Elements:
· All outputs to the user must include identifying text.
· The user should be prompted for all inputs.
· Your program file must meet the programming standards defined for this course and contain the appropriate header defined for this course.
· Include Honor Pledge on all files and document all methods
Expected Output (Input Underlined):
Creating HourlyEmployee…
12345
Rogers, Steve A.
Gender: M
Status: Full Time
Wage: 15.34
Hours Worked: 0.00
Creating SalaryEmployee...
54321
Pryde, Kitty X.
Gender: F
Status: Full Time
Salary: 75000.00
Creating CommissionEmployee...
Employee Number invalid, please re-enter:
33333
33333
Storm, Johnny F.
Gender: F
Status: Part Time
Rate: 2.50
Sales: 0.00
Increasing Hourly\'s hours worked by 50.
Increasing Commissions\'s sales by 150,000.
Hourly Payout: 920.40
Salary Payout: 1442.31
Commission Payout: 3750.00
Finding total bonus payout...
Bonus Payout is 2863.60
Applying annual raises and resetting week...
12345
Rogers, Steve A.
Gender: M
Status: Full Time
Wage: 16.10
Hours Worked: 0.00
54321
Pryde, Kitty X.
Gender: F
Status: Full Time
Salary: 79500.00
33333
Storm, Johnny F.
Gender: F
Status: Part Time
Rate: 2.70
Sales: 0.00
Driver:
| Employee |
| - String firstName - String lastName - char middleInitial - boolean fulltime - char gender - int employeeNum |
| <<constructor>> Employee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean ) + getEmployeeNumber() : int + setEmployeeNumber(empNum : int) + getFirstName() : String + getLastName() : String + getMiddleInitial() : char + getGender() : char + setFirstName(fn: String) + setLastName(ln : String) + setMiddleI(m : char) + setGender(g : char) + equals(e2 : Object) : boolean + toString() : String |
Solution
Employee.java:
package employeeType.employee;
import java.util.Scanner;
public class Employee {
private String firstName;
private String lastName;
private char middleInitial;
private boolean fulltime;
private char gender;
private int employeeNum;
public Employee(String firstName, String lastName, char middleInitial,
char gender, int employeeNum, boolean fulltime) {
setFirstName(firstName);
setLastName(lastName);
setMiddleInitial(middleInitial);
setGender(gender);
setEmployeeNum(employeeNum);
this.fulltime = fulltime;
}
public int getEmployeeNum() {
return employeeNum;
}
public void setEmployeeNum(int employeeNum) {
Scanner s = new Scanner(System.in);
while (employeeNum < 10000 || employeeNum > 99999) {
System.out.println(\"Employee Number invalid, please re-enter\");
employeeNum = s.nextInt();
}
this.employeeNum = employeeNum;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public char getMiddleInitial() {
return middleInitial;
}
public char getGender() {
return gender;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setMiddleInitial(char middleInitial) {
this.middleInitial = middleInitial;
}
public void setGender(char gender) {
if (gender != \'M\') {
gender = \'F\';
}
this.gender = gender;
}
@Override
public boolean equals(Object e2) {
return fulltime;
}
@Override
public String toString() {
return employeeNum + \"\ \" + lastName + \", \" + firstName + \" \" + middleInitial + \".\ Gender: \"
+ gender + \"\ Status:\" + (fulltime ? \"Full Time\" : \"Part Time\");
}
}
HourlyEmployee.java
package employeeType.subTypes;
import employeeType.employee.Employee;
public class HourlyEmployee extends Employee{
private double wage;
private double hoursWorked;
public HourlyEmployee(String firstName, String lastName, char middleInitial, char gender, int employeeNum,
boolean fulltime, Double wage) {
super(firstName, lastName, middleInitial, gender, employeeNum, fulltime);
hoursWorked = 0.0;
// to round to 2 places
this.wage = Math.round(wage * 100.0) / 100.0;
}
public void annualRaise(){
// to round to 2 places after 5% increase
wage = wage * 1.05;
}
public double calculateWeeklyPay(){
double payment = hoursWorked * wage;
// if hourse worked are more than 40 hours
if(hoursWorked > 40) {
payment += (hoursWorked - 40)*wage;
}
// to round to 2 places
payment = Math.round(payment * 100.0) / 100.0;
return payment;
}
public double holidayBonus(){
return 40* wage;
}
public void increaseHours(double hours){
if(hours > 0) {
this.hoursWorked += hours;
} else {
System.out.println(\"Hours worked should be greater than 0.\");
}
}
public void resetWeek() {
hoursWorked = 0;
}
@Override
public String toString() {
return super.toString() + \"\ Wage:\" + wage + \"\ Hours Worked: \"+ hoursWorked;
}
}
SalaryEmployee.java
package employeeType.subTypes;
import employeeType.employee.Employee;
public class SalaryEmployee extends Employee {
private double salary;
public SalaryEmployee(String firstName, String lastName,
char middleInitial, char gender, int employeeNum, boolean fulltime,
Double salary) {
super(firstName, lastName, middleInitial, gender, employeeNum, fulltime);
this.salary = Math.round(salary * 100.0) / 100.0;
}
public double calculateWeeklyPay() {
// to round to 2 places
return Math.round(salary / 52 * 100.0) / 100.0;
}
public void annualRaise() {
salary = salary * 1.06;
// to round to 2 places
salary = Math.round(salary * 100.0) / 100.0;
}
public double holidayBonus() {
// to round to 2 places
return Math.round(0.03 * salary * 100.0) / 100.0;
}
public void resetWeek() {
}
@Override
public String toString() {
return super.toString() + \"\ Salary:\" + salary;
}
}
CommissionEmployee.java
package employeeType.subTypes;
import employeeType.employee.Employee;
public class CommissionEmployee extends Employee {
private double sales;
private double rate;
public CommissionEmployee(String firstName, String lastName,
char middleInitial, char gender, int employeeNum, boolean fulltime,
Double rate) {
super(firstName, lastName, middleInitial, gender, employeeNum, fulltime);
this.rate = rate;
this.sales = 0.00;
}
public double calculateWeeklyPay() {
// to round to 2 places
return Math.round(sales * rate / 100 * 100.0) / 100.0;
}
public void annualRaise() {
rate += 0.2;
}
public double holidayBonus() {
return 0;
}
public void increaseSales(double sales) {
if (sales > 0) {
this.sales += sales;
} else {
System.out.println(\"Sales should be greater than 0.\");
}
}
public void resetWeek(){
sales = 0.00;
}
@Override
public String toString() {
return super.toString() + \"\ Rate: \" + rate + \"\ Sales: \" + sales;
}
}
EmployeeTest.java:
import employeeType.subTypes.CommissionEmployee;
import employeeType.subTypes.HourlyEmployee;
import employeeType.subTypes.SalaryEmployee;
public class EmployeeTest
{
public static void main(String args[])
{
//Create references for our objects
HourlyEmployee hourly;
SalaryEmployee salary;
CommissionEmployee commission;
//The sum of all bonuses will be placed here
double bonusPayout = 0;
//Create and print a new HourlyEmployee
hourly = HourlyFactory();
System.out.printf(\"\ %s\ \", hourly);
//Create and print a new SalaryEmployee
salary = SalaryFactory();
System.out.printf(\"\ %s\ \", salary);
//Create and print a new CommissionEmployee
commission = CommissionFactory();
System.out.printf(\"\ %s\ \", commission);
//Alter some values of Hourly and Commission
System.out.println(\"\ Increasing Hourly\'s hours worked by 50.\");
hourly.increaseHours(50);
System.out.println(\"Increasing Commissions\'s sales by 150,000.\ \");
commission.increaseSales(150000);
//Output weekly pay for each Employee
System.out.printf(\"Hourly Payout: %.2f\ \", hourly.calculateWeeklyPay());
System.out.printf(\"Salary Payout: %.2f\ \", salary.calculateWeeklyPay());
System.out.printf(\"Commission Payout: %.2f\ \ \", commission.calculateWeeklyPay());
//Find total bonus payout
System.out.println(\"Finding total bonus payout...\");
bonusPayout += hourly.holidayBonus();
bonusPayout += salary.holidayBonus();
bonusPayout += commission.holidayBonus();
System.out.printf(\"Bonus Payout is %.2f\ \ \", bonusPayout);
//Reset the week and apply raises to all Employees
System.out.println(\"Applying annual raises and resetting week...\");
hourly.resetWeek();
hourly.annualRaise();
salary.resetWeek();
salary.annualRaise();
commission.resetWeek();
commission.annualRaise();
//Output Employees after changes
System.out.printf(\"%s\ \ %s\ \ %s\ \", hourly, salary, commission);
}
//Retrieve input and create HourlyEmployee
public static HourlyEmployee HourlyFactory()
{
System.out.println(\"Creating HourlyEmployee...\");
return new HourlyEmployee(\"Steve\", \"Rogers\", \'A\', \'M\', 12345, true, 15.34);
}
//Retrieve input and create SalaryEmployee
public static SalaryEmployee SalaryFactory()
{
System.out.println(\"\ Creating SalaryEmployee...\");
return new SalaryEmployee(\"Kitty\", \"Pryde\", \'X\', \'F\', 54321, true, 75000.0);
}
//Retrieve input and create CommissionEmployee
public static CommissionEmployee CommissionFactory()
{
System.out.println(\"\ Creating CommissionEmployee...\");
return new CommissionEmployee(\"Johnny\", \"Storm\", \'F\', \'L\', 976499, false, 2.5);
}
}
Sample Output:
Creating HourlyEmployee...
12345
Rogers, Steve A.
Gender: M
Status:Full Time
Wage:15.34
Hours Worked: 0.0
Creating SalaryEmployee...
54321
Pryde, Kitty X.
Gender: F
Status:Full Time
Salary:75000.0
Creating CommissionEmployee...
Employee Number invalid, please re-enter
33333
33333
Storm, Johnny F.
Gender: F
Status:Part Time
Rate: 2.5
Sales: 0.0
Increasing Hourly\'s hours worked by 50.
Increasing Commissions\'s sales by 150,000.
Hourly Payout: 920.40
Salary Payout: 1442.31
Commission Payout: 3750.00
Finding total bonus payout...
Bonus Payout is 2863.60
Applying annual raises and resetting week...
12345
Rogers, Steve A.
Gender: M
Status:Full Time
Wage:16.107
Hours Worked: 0.0
54321
Pryde, Kitty X.
Gender: F
Status:Full Time
Salary:79500.0
33333
Storm, Johnny F.
Gender: F
Status:Part Time
Rate: 2.7
Sales: 0.0












