You need to add a new private instance field \"birthDate\" to the Employee class
ID: 3665633 • Letter: Y
Question
You need to add a new private instance field "birthDate" to the Employee class. The “birthDate” field must be a Date data type, not a String! Add get and set methods to Employee class for this new birthDate field.
The birthDate field must be determined from separate integer values, supplied by the user, for the birth month, day, and year. As with all other inputs, these three birth date components must be prompted from and input to class PayrollSystemTest. Once input, these three parameter values must then be supplied to the appropriate modified employee subclass constructor, and then the subclass constructors supply these three values to their parent class, a modified Employee class constructor.
The single birthDate field of a Date data type must be declared and initialized in the Employee class.
In class PayrollSystemTest create an array of Employee variables to store references to the various employee objects. In a loop, calculate the monthly paycheck amount for each Employee (polymorphically), and add a $100.00 bonus to the person's monthly payroll amount if the current month (i.e. November) is the month in which the Employee's birthday occurs.
Your program should input data and create the five employees below on the version of the program you submit for grading. Your program must calculate their monthly salaries for November (assumed to be the "current" month). Assume that the monthly salary is simply four times the weekly salary.
Explanation / Answer
Employee Superclass
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private String birthDate;
// three-argument constructor
public Employee( String first, String last, String ssn, String date, int month, int day, int year )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = date;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // 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( String date)
{
birthDate = date;
}
public String getBirthDate()
{
return birthDate;
}
// return String representation of Employee object
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 subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
Date class:
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
public String toDateString(String birthDate)
{
return String.format( "Birthdate: %d/%d/%d", month, day, year );
}
} // end class Date
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.