I wrote this class Appointment2, and need to build Appointment2 objects that utilize my Date class (I included both Appointment2 and Date below) assuming that I am creating the object correctly, how would I go about writing a setDate method.
I the main method of my Appoinment2 class, I am creating a Date
Date date1 = new Date;
and then passing that date reference as a part of my appointment constructor. I am not sure that this is the way to do this.
[/CODE]public class Appointment2
{
private Date date;
private Time time;
private
String location;
private String appointed;
private String purpose;
public Appointment2(Date date, Time time, String location, String appointed, String purpose)
{
this.date = date;
this.time = time;
this.location = location;
this.appointed = appointed;
this.purpose = purpose;
}
public Appointment2()
{
this.date = date;
this.time = time;
this.location = location;
this.appointed = appointed;
this.purpose = purpose;
}
public Date getDate()
{
return date;
}
public static void main (String[]args) throws Exception
{
Date date1 = new Date(2004, 11, 22);
Time time1 = new Time(12, 3, 23);
Appointment2 aAppt = new Appointment2(date1, time1, "Suite 201", "Crenshaw", "Checkup");
System.out.println(aAppt.getDate());
}
}[CODE]
[CODE]
public class Date
{
protected int year;
protected int month;
protected int day;
public Date(int year, int month, int day) throws Exception
{
if (year < 0 || year > 8089)
throw new Exception("***Year must be > 0 and < 8089");
if (month > 12 || month < 1)
throw new Exception("***Month must be less than 12 and greater than 0");
if (day < 1 || day > 31)
throw new Exception("***Day must be greater than 1 and less than 31");
if (month == 9 && day > 30 || month == 4 && day > 30 || month == 11 && day > 30 )
throw new Exception("***( Months 4, 9. and 11 only has 30 days");
if (year % 4 != 0 && month == 2 && day > 28 )
throw new Exception("***( Month 2 has 28 days");
if (year % 4 == 0 && month == 2 && day > 29)
throw new Exception("***You cannot have more than 29 days in 2");
this.year = year;
this.month = month;
this.day = day;
}
public Date()
{
this.year = year;
this.month = month;
this.day = day;
}
static Date getDate(int year, int month, int day)
{
if (year < 0 || year > 8089)
return null;
if (month > 12 || month < 1)
return null;
if (day < 1 || day > 31)
return null;
try
{
return new Date(year, month, day);
}
catch(Exception e)
{
//convincing compiler we are handling exception
}
return null;
}
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public void setDate(int year, int month, int day) throws Exception
{
if (year < 0 || year > 8089)
throw new Exception("***Year must be > 0 and < 8089");
if (month > 12 || month < 1)
throw new Exception("***Month must be less than 12 and greater than 0");
if (day < 1 || day > 31)
throw new Exception("***Day must be greater than 1 and less than 31");
if (month == 9 && day > 30 || month == 4 && day > 30 || month == 11 && day > 30 )
throw new Exception("***( Months 4, 9. and 11 only has 30 days");
if (year % 4 != 0 && month == 2 && day > 28 )
throw new Exception("***( Month 2 has 28 days");
if (year % 4 == 0 && month == 2 && day > 29)
throw new Exception("***You cannot have more than 29 days in 2");
return;
}
public String toString()
{
return "The date is: year:" + year + " month:" + month + " day:" + day;
}
public boolean equals( Date otherDate )
{
return getYear() == (otherDate.getYear())&&
getDay() == (otherDate.getDay()) &&
getMonth() == (otherDate.getMonth());
}
public int compareTo ( Date otherDate )
{
if (getYear() != (otherDate.getYear()))
return getYear() - (otherDate.getYear());
else if (getMonth() != (otherDate.getMonth()))
return getMonth() - (otherDate.getMonth());
else
return getDay() - (otherDate.getDay());
}
public Date tomorrow()
{
getDay();
if (month == 9 && day == 30 || month == 4 && day == 30 || month == 11 && day == 30 ) {
day = 1;
month++;
} else if (year % 4 != 0 && month == 2 && day == 28 ) {
day = 1;
month = 2;
} else if (year % 4 == 0 && month == 2 && day == 29) {
day = 1;
month = 3;
} else {
day++;
}
return null;
}
}