public class Date1 extends Object{
private int month,day,year;
public Date1(int m,int d,int y){
month = checkMonth(m);
year = y;
day = checkDay(d);
System.out.println("Date object for date:"+toDateString());
} // end Date1 constructor
public int checkMonth(int testm){
if(testm>=0&&testm<=12)
return testm;
else{
System.out.println("Invalid month("+testm+") set to 1.");
return 1;
}
}
public int checkDay(int testd){
int daysPerMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testd>=0&&testd<=daysPerMonth[month])
return testd;
if(month==2&&testd==29&&(year%400==0||(year%4==0&&year%100!=0)))
return testd;
System.out.println("Invalid day("+testd+") set to 1.");
return 1;
}
public String toDateString(){
return month+"/"+day+"/"+year;
}
}
public class Employee extends Object{
private String firstName,lastName;
private Date1 birthDate,hireDate;
public Employee(String first,String last,Date1 birthOfDate,Date1 hireOfDate){
firstName = first;
lastName = last;
birthDate = birthOfDate;
hireDate = hireOfDate;
} // end Employee constructor
public String toEmployeeString(){
return firstName+" "+lastName+
" BirthDate: "+birthDate.toDateString()+
" , Hired in: "+hireDate.toDateString();
} // end toEmployeeString method
}
import javax.swing.*;
public class EmployeeTest{
public static void main(String args[]){
String out = "";
Date1 birth = new Date1(2,29,1988);
Date1 hire = new Date1(3,31,2006);
Employee employee = new Employee("John ","Bob ,",birth,hire);
out += employee.toEmployeeString();
JOptionPane.showMessageDialog(null,out);
System.exit(0);
}
}