• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom Date class and Appointment class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

}
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... your code tags buggered out, methinks.
Anyway, it's simple to add teh set method:

This will set the date.
As to whether or not is better to set the date in the constructor or thought the set method: it depends on how you are going to use the class. It strikes me that it would make sense to have both methods. You typically would not create an Appointment without a Date, but that Date could change.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One comment:
I typically do not allow local variables to have the same names as member variables; this can lead to quite a bit of confusion. While some people make an exception to this rule for parameters of constructors, I do not. As such, I would write:

Doing this, you will note that the body of your other constuctor is, well, rather pointless:

It will, of course, compile and run. It just doesn't do anything. If you want a no-argument constructor, you don't actually need to provide the body; an empty method will do.
 
Gerald Spica
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made the changes as described, and this is where I get lost, I have two calls to the setDate()

date1.setDate(2001, 2, 28); which does not change the date for aAppt
aAppt.setDate(2001, 2, 28); which gives this compile error
C:\j2sdk1.4.1_03\dev>javac Appointment2.java
Appointment2.java:58: setDate(Date) in Appointment2 cannot be applied to (int,in
t,int)
aAppt.setDate(2001, 2, 28);
^
1 error
My conundrum is, if a Date object consists of (int year, int day, int month)
Why can't I pass it those values. This is the basis of my confusion.
 
Gerald Spica
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shoot that last code segment got messed up, see main method - specifically
date1.setDate(2001, 2, 28); //this does not change the date
aAppt.setDate(2001, 2, 28); //this gives the following compile error
System.out.println(date1.getDate());
C:\j2sdk1.4.1_03\dev>javac Appointment2.java
Appointment2.java:58: setDate(Date) in Appointment2 cannot be applied to (int,int,int)
aAppt.setDate(2001, 2, 28);
^
1 error

 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah--it doesn't work because it can't find a setDate(int, int, int) in your appointment class. Your appointment class is expecting a Date object as the parameter, but you are trying to pass it three ints. Java is smart, but not that smart (and frankly, the compiler is pretty dumb. That is on purpose. The theory is that there is only a certain amount of dumbness to go around, and so if the compiler is really dumb, there won't be enough dumbness left for you to make dumb mistakes... )
You have two options. The first is to create such a method in your appointments class. This method would take the three parameters and set the Appointment's date equal to a new Date(year, month, day);
The second solution, which is probably the better one, is to change the way you call the function:
 
Gerald Spica
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks, that works. I am still confused however and I think it is becuase I am assuming Java is smarter than it is. What I was trying to do before was to have an Appointment2 constructor that took Date date, and created a Date object made up of the three int's, (int year, int month, int day) and then those are available as a part of my Appointment2 object, being available meaning I was free to change them anytime I want. It was an epiphany for me to realize I needed to create a Date date1 = new Date(2002, 1, 2) and then in my constructor use that name date1 as the parameter.
For some reason it seems redundant to me, or unnatural. It makes me believe there must be a better way.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic