Class Date This composition Lab contains classes Date, and Employee. Class Date
ID: 3681829 • Letter: C
Question
Class Date
This composition Lab contains classes Date, and Employee. Class Date declares private variables month, day and year to represent a date. The constructor receives three int parameters. The method checkMonth uses to validate the month—if the value is out-of-range. The method checkDay is to validate the day based on the current month and year. Determines whether the day is correct based on the number of days in the particular month. If the day is not correct.
Class Employee
Class Employee has private variables firstName, lastName, birthDate and hire-Date. The Members firstName and lastName are String. Members birthDate and hireDate are references to Date objects. This demonstrates that a class can have as instance variables references to objects of other classes (Composition). The Employee constructor takes four parameters—first, last, dateOfBirth and dateOf-Hire.
The objects referenced by the parameters are assigned to the Employee object’s private variables.
Main function
Creates two Employee objects and initializes the data members by passing to the constructor and (first and last names) and (the birthday and hire date).
Display the values of its private variables and demonstrate that the object was initialized properly.
Explanation / Answer
EmployeeTest.java
public class EmployeeTest {
public static void main(String[] args)
{
Date birth = new Date(7,24,1949);
Date hire = new Date(3,12,1988);
Employee employee = new Employee("Bob", "Blue", birth, hire);
System.out.println(employee);
}
}
Date.java
public class Date {
private int month;
private int day;
private int year;
private static final int[] daysPerMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public Date(int month, int day, int year)
{
//check if month is in range
if (month <= 0 || month > 12)
throw new IllegalArgumentException("month (" + month + ") must be 1-12");
//check if day is in range
if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("day (" + day + ")out-of-range for the specified month and year");
if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 !=0)))
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
this.month = month;
this.day = day;
this.year = year;
System.out.printf("Date object constructor for date %s%n", this);
}
public String toString()
{
return String.format("%d/%d/%d", month,day,year);
}
}
Employee.java
public class Employee {
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;
public Employee(String firstName, String lastName, Date birthDate, Date hireDate)
{
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
}
public String toString()
{
return String.format ("%s, %s Hired: %s Birthday: %s", lastName, firstName, hireDate, birthDate);
}
}
sample output
Date object constructor for date 7/24/1949
Date object constructor for date 3/12/1988
Blue, Bob Hired: 3/12/1988 Birthday: 7/24/1949