Hello this is a java question. Thanks in advance! The question reads Modify the
ID: 3757874 • Letter: H
Question
Hello this is a java question. Thanks in advance! The question reads
Modify the payroll system to include private instance variable birthDate in class Employee. Use class Date to represent an employee's birthday. Add get methods to class Date. Assume that payroll is processed once per month. Create an array of Employee variable to store references to the various employee objects. In a loop, calculate the payroll for each Employee (polymorphically), and add a $100.00 bonus to the person's payroll amount if the current month is the one in which the Employee's birthday occurs.
Below are the is the code to be edited
public class PayrollSystemTest
{
public static void main( String[] args )
{
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
System.out.println( "Employees processed individually: " );
System.out.printf( "%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings() );
// create four-element Employee array
Employee[] employees = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
System.out.println( "Employees processed polymorphically: " );
// generically process each element in array employees
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
// determine whether element is a BasePlusCommissionEmployee
if ( currentEmployee instanceof BasePlusCommissionEmployee )
{
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
( BasePlusCommissionEmployee ) currentEmployee;
employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary() );
} // end if
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
****************************************************************************
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // 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
// return String representation of Employee object
@Override
public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
*********************************************************************
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn,
double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // 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
********************************************************************
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,
double hourlyWage, double hoursWorked )
{
super( first, last, ssn );
setWage( hourlyWage ); // validate and store hourly wage
setHours( hoursWorked ); // validate and store hours worked
} // end five-argument HourlyEmployee constructor
// set wage
public void setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set hours worked
public void setHours( double hoursWorked )
{
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.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 ) // no overtime
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
*******************************************************
public class CommissionEmployee extends Employee
{
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee( String first, String last, String ssn,
double sales, double rate )
{
super( first, last, ssn );
setGrossSales( sales );
setCommissionRate( rate );
} // end five-argument CommissionEmployee constructor
// set commission rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// set gross sales amount
public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee object
@Override
public String toString()
{
return String.format( "%s: %s %s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
} // end method toString
} // end class CommissionEmployee
********************************************************************
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; // non-negative
} // end method setBaseSalary
// return base salary
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings; override method earnings in CommissionEmployee
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee object
@Override
public String toString()
{
return String.format( "%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary() );
} // end method toString
} // end class BasePlusCommissionEmployee
************************************************************
public class Date
{
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// 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 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
{
System.out.printf(
"Invalid month (%d) set to 1.", testMonth );
return 1; // maintain object in consistent state
} // end else
} // end method checkMonth
// utility method to confirm proper day value based on month and year
private int checkDay( int testDay )
{
int[] daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 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;
System.out.printf( "Invalid day (%d) set to 1.", testDay );
return 1; // maintain object in consistent state
} // end method checkDay
// 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
Explanation / Answer
package current;
public class PayrollSystemTest {
public static void main(String[] args) {
// create subclass objects
SalariedEmployee salariedEmployee = new SalariedEmployee("John",
"Smith", "111-11-1111", 800.00);
//setting birth date
salariedEmployee.setBirthDate(new Date(10, 1, 1984));
HourlyEmployee hourlyEmployee = new HourlyEmployee("Karen", "Price",
"222-22-2222", 16.75, 40);
CommissionEmployee commissionEmployee = new CommissionEmployee("Sue",
"Jones", "333-33-3333", 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300);
System.out.println("Employees processed individually: ");
System.out.printf("%s %s: $%,.2f ", salariedEmployee, "earned",
salariedEmployee.earnings());
System.out.printf("%s %s: $%,.2f ", hourlyEmployee, "earned",
hourlyEmployee.earnings());
System.out.printf("%s %s: $%,.2f ", commissionEmployee, "earned",
commissionEmployee.earnings());
System.out.printf("%s %s: $%,.2f ", basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings());
// create four-element Employee array
Employee[] employees = new Employee[4];
// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;
System.out.println("Employees processed polymorphically: ");
// generically process each element in array employees
for (Employee currentEmployee : employees) {
System.out.println(currentEmployee); // invokes toString
// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee) {
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee;
employee.setBaseSalary(1.10 * employee.getBaseSalary());
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary());
} // end if
//Lets take current month - change this as per your need
int currentMonth = (new java.util.Date()).getMonth()+1; //otherwise hard code to some value
System.out.println("currentMonth : "+currentMonth);
double bonus = 0;
if(currentEmployee.getBirthDate() != null) {
if(currentEmployee.getBirthDate().getMonth() == currentMonth ) {
System.out.println("Birth Month matched ");
bonus = 100;
}
}
System.out.printf("earned $%,.2f ", bonus+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
// ****************************************************************************
abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
//Adding birthdate
private Date birthDate;
// three-argument constructor
public Employee(String first, String last, String ssn) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = null;
} // end three-argument Employee constructor
// four-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
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
// 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
// return String representation of Employee object
@Override
public String toString() {
return String.format("%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
} // end method toString
// abstract method overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
// *********************************************************************
class SalariedEmployee extends Employee {
private double weeklySalary;
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn, double salary) {
super(first, last, ssn); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor
public SalariedEmployee(String first, String last, String ssn, double salary, Date birthDate) {
super(first, last, ssn,birthDate); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
}
// set salary
public void setWeeklySalary(double salary) {
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // 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
// ********************************************************************
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,
double hourlyWage, double hoursWorked) {
super(first, last, ssn);
setWage(hourlyWage); // validate and store hourly wage
setHours(hoursWorked); // validate and store hours worked
} // end five-argument HourlyEmployee constructor
public HourlyEmployee(String first, String last, String ssn,
double hourlyWage, double hoursWorked, Date birthDate) {
super(first, last, ssn, birthDate);
setWage(hourlyWage); // validate and store hourly wage
setHours(hoursWorked); // validate and store hours worked
}
// set wage
public void setWage(double hourlyWage) {
wage = (hourlyWage < 0.0) ? 0.0 : hourlyWage;
} // end method setWage
// return wage
public double getWage() {
return wage;
} // end method getWage
// set hours worked
public void setHours(double hoursWorked) {
hours = ((hoursWorked >= 0.0) && (hoursWorked <= 168.0)) ? hoursWorked
: 0.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) // no overtime
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
// *******************************************************
class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee(String first, String last, String ssn,
double sales, double rate) {
super(first, last, ssn);
setGrossSales(sales);
setCommissionRate(rate);
} // end five-argument CommissionEmployee constructor
public CommissionEmployee(String first, String last, String ssn,
double sales, double rate, Date birthDate) {
super(first, last, ssn, birthDate);
setGrossSales(sales);
setCommissionRate(rate);
}
// set commission rate
public void setCommissionRate(double rate) {
commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate() {
return commissionRate;
} // end method getCommissionRate
// set gross sales amount
public void setGrossSales(double sales) {
grossSales = (sales < 0.0) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales() {
return grossSales;
} // end method getGrossSales
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings() {
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee object
@Override
public String toString() {
return String.format("%s: %s %s: $%,.2f; %s: %.2f",
"commission employee", super.toString(), "gross sales",
getGrossSales(), "commission rate", getCommissionRate());
} // end method toString
} // end class CommissionEmployee
// ********************************************************************
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
public BasePlusCommissionEmployee(String first, String last, String ssn,
double sales, double rate, double salary, Date birthDate) {
super(first, last, ssn, sales, rate, birthDate);
setBaseSalary(salary); // validate and store base salary
}
// set base salary
public void setBaseSalary(double salary) {
baseSalary = (salary < 0.0) ? 0.0 : salary; // non-negative
} // end method setBaseSalary
// return base salary
public double getBaseSalary() {
return baseSalary;
} // end method getBaseSalary
// calculate earnings; override method earnings in CommissionEmployee
@Override
public double earnings() {
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee object
@Override
public String toString() {
return String.format("%s %s; %s: $%,.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary());
} // end method toString
} // end class BasePlusCommissionEmployee
// ************************************************************
class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// 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 year
day = checkDay(theDay); // validate day
System.out.printf("Date object constructor for date %s ", this);
} // end Date constructor
public int getMonth() {
return month;
}
// 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
{
System.out.printf("Invalid month (%d) set to 1.", testMonth);
return 1; // maintain object in consistent state
} // end else
} // end method checkMonth
// utility method to confirm proper day value based on month and year
private int checkDay(int testDay) {
int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
// 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;
System.out.printf("Invalid day (%d) set to 1.", testDay);
return 1; // maintain object in consistent state
} // end method checkDay
// 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
Added code in the polymorphic loop iteration: Highhlighted the salary
----------output---------------: Look for employee : John Smith----------------------
Date object constructor for date 10/1/1984
Employees processed individually:
salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
earned: $800.00
hourly employee: Karen Price
social security number: 222-22-2222
hourly wage: $16.75; hours worked: 40.00
earned: $670.00
commission employee: Sue Jones
social security number: 333-33-3333
gross sales: $10,000.00; commission rate: 0.06
earned: $600.00
base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
earned: $500.00
Employees processed polymorphically:
salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
currentMonth : 10
Birth Month matched
earned $900.00
hourly employee: Karen Price
social security number: 222-22-2222
hourly wage: $16.75; hours worked: 40.00
currentMonth : 10
earned $670.00
commission employee: Sue Jones
social security number: 333-33-3333
gross sales: $10,000.00; commission rate: 0.06
currentMonth : 10
earned $600.00
base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
new base salary with 10% increase is: $330.00
currentMonth : 10
earned $530.00
Employee 0 is a current.SalariedEmployee
Employee 1 is a current.HourlyEmployee
Employee 2 is a current.CommissionEmployee
Employee 3 is a current.BasePlusCommissionEmployee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.