(Creating a New Subclass of Employee) Other types of Employees might include Sal
ID: 3694183 • Letter: #
Question
(Creating a New Subclass of Employee) Other types of Employees might include SalariedEmployees who get paid a fixed weekly salary, PieceWorkers who get paid by the number of pieces they produce or HourlyEmployees who get paid an hourly wage with time-and-a-half—1.5 times the hourly wage—for hours worked over 40 hours.
Create class HourlyEmployee that inherits from class Employee (Exercise 9.14) and has instance variable hours (a double) that represents the hours worked, instance variable wage (a double) that represents the wages per hour, a constructor that takes as arguments a first name, a lastname, a social security number, an hourly wage and the number of hours worked, set and get methods for manipulating the hours and wage, an earnings method to calculate an HourlyEmployee’s earnings based on the hours worked and a toString method that returns the HourlyEmployee’s String representation. Method setWage should ensure that wage is nonnegative, and setHours should ensure that the value of hours is between 0 and 168 (the total number of hours in a week).
Use class HourlyEmployee in a test program that’s similar to the one in Fig. 9.5
I want the correct format for Eclipse program
Explanation / Answer
Pieceworker.java
public class Pieceworker extends Employee
{
private double wage;
private int pieces;
public Pieceworker(String first, String last, String ssn,
Date bdate, double w, int p)
{
super(first, last, ssn, bdate);
setWage(w);
setPieces(p);
}
public void setWage(double w)
{
wage = w;
}
public void setPieces(int p)
{
pieces = p;
}
public double getWage()
{
return wage;
}
public int getPieces()
{
return pieces;
}
@Override
public double earnings() {
return getPieces() * getWage();
}
@Override
public String toString()
{
return String.format("Pieceworker: %s %s: $%,.2f %s: %d",
super.toString(), "Wage", getWage(), "Pieces", getPieces());
} // end method toString
}
SalariedEmployee.java
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn,
Date bdate, double salary)
{
super(first, last, ssn, bdate); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary(double salary)
{
if (salary >= 0.0)
weeklySalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
@Override
public String toString()
{
return String.format("salaried employee: %s %s: $%, .2f",
super.toString(), "weekly salary", getWeeklySalary());
} // end method toString
} // end class SalariedEmployee
Employee.java
// Employee abstract superclass
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDate;
// three-argument constructor
public Employee(String first, String last, String ssn, Date bdate)
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = bdate;
} // end three-argument Employee constructor
// set first name
public void setFirstName(String first)
{
firstName = first; // should validate
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName(String last)
{
lastName = last; // should validate
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber(String ssn)
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
public void setBirthDate(Date bdate)
{
birthDate = bdate;
}
public Date getBirthDate()
{
return birthDate;
}
// return String representation of Employee object
@Override
public String toString()
{
return String.format("%s %s Social Security Number: %s Birthdate: %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthDate());
} // end method toString
// abstract method overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employees
HourlyEmployee.java
// HourlyEmployee class extends Employee
public class HourlyEmployee extends Employee
{
private double wage; // wage per hour
private double hours; // hours worked for week
// five-argument constructor
public HourlyEmployee(String first, String last, String ssn,
Date bdate, double hourlyWage, double hoursWorked)
{
super(first, last, ssn, bdate);
setWage(hourlyWage); // validate hourly wage
setHours(hoursWorked); // validate hours worked
} // end five-argument HourlyEmployee constructor
// set wage
public void setWage(double hourlyWage)
{
if (hourlyWage >= 0.0)
wage = hourlyWage;
else
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set hours worked
public void setHours(double hoursWorked)
{
if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0))
hours = hoursWorked;
else
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
} // end method setHours
// return hours worked
public double getHours()
{
return hours;
} // end method getHours
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
if ( getHours() <= 40)
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40) * getWage() * 1.5;
} // end method earnings
// return String representation of HourlyEmployee object
@Override
public String toString()
{
return String.format("hourly employee: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours() );
} // end method toString
} // end class HourlyEmployee
Date.java
// Date class declaration.
public class Date
{
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
private static final int[] daysPerMonth = // days in each month
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 , 31};
// constructor: call checkMonth to confirm proper value for month;
// call checkDay to confirm proper value for day
public Date(int theMonth, int theDay, int theYear)
{
month = checkMonth(theMonth); // validate month
year = theYear; // could validate the year
day = checkDay( theDay); // validate day
System.out.printf(
"Date object constructor for date %s ", this);
} // end Date constructor
// utility method to confirm proper month value
private int checkMonth(int testMonth)
{
if ( testMonth > 0 && testMonth <= 12) // validate month
return testMonth;
else // month is invalid
throw new IllegalArgumentException("month must be 1-12");
} // end method checkMonth
// utility method to confirm proper day value based on month and year
private int checkDay(int testDay)
{
// check if day in range for month
if (testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
// check for leap year
if ( month == 2 && testDay == 29 && (year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
throw new IllegalArgumentException(
"day out-of-range for the specified month and year");
} // end method checkDay
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
};
// return a String of the form month/day/year
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
} // end method toString
} // end class Date
Main.java
// Employee hierarchy test program.
public class Main
{
public static void main(String[] args)
{
int currentMonth = 3;
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee("John", "Smith", "111-11-1111", new Date(1,31,1990), 8000.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee("Karen", "Price", "222-22-2222", new Date(2,28,1990), 16.75, 40);
Pieceworker pieceworker =
new Pieceworker(
"James", "Tran", "555-55-5555", new Date(7,2,1997), .60, 1500);
// create four-element Employee array
Employee[] employees = new Employee[5];
// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
// employees[2] = commissionEmployee;
// employees[3] = basePlusCommissionEmployee;
employees[4] = pieceworker;
System.out.println("Employees plocessed polymorphically: ");
// generically process each element in array employees
for ( Employee currentEmployee : employees)
{
System.out.println(currentEmployee); // invokes toString
System.out.printf(
"earned $%,.2f ", currentEmployee.earnings());
} // end for
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++)
System.out.printf("Employee %d is a %s ", j,
employees[j].getClass().getName());
} // end main
} // end class PayrollSystemTest
sample output
Date object constructor for date 1/31/1990
Date object constructor for date 2/28/1990
Date object constructor for date 7/2/1997
Employees plocessed polymorphically:
salaried employee: John Smith
Social Security Number: 111-11-1111
Birthdate: 1/31/1990
weekly salary: $ 8,000.00
earned $8,000.00
hourly employee: Karen Price
Social Security Number: 222-22-2222
Birthdate: 2/28/1990
hourly wage: $16.75; hours worked: 40.00
earned $670.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.