Hello,
I had put this question in another thread and I got a response that seemed to explain it well but I just seem to keep going in circles and I'm still just not getting it. So I need some more help. The other thread started getting pretty lengthy so I just started a new one. Here's what I am supposed to do:
In addition you need to add a new private instance field "birthDate" to the Employee class. Add get and set methods to Employee class for this new birthDate field. The birthdate is to be displayed using a customized toDateString method in the Date class. The birthDate field must be determined from separate integer values, supplied by the user, for the month, day, and year. These three parameter values should be supplied to a modified Employee class constructor, where the single birthDate field is initialized.
Here is the ORIGINAL Employee superclass code:
// Employee.java
// Employee abstract superclass.
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;
} // 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
// return String representation of Employee object
public String toString()
{
return String.format( "%s %s\nsocial security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
Here is the ORIGINAL Date class:
// 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
// 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\n", 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
And here are my modified versions of the Employee and Date classes. They build fine but when I try to use them with my main class, it seems obvious that I've still done something wrong. (I get something like a "no such method exists" message when I run it.) Can anyone help me?
// Employee.java
// Employee abstract 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\nsocial security number: %s\nBirthdate: %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthDate() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
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\n", 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