• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creation and usage of a class  RSS feed

 
Robert Richard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a class that does various things with given dates. In that class I attempted to write a method the will calculate the month/day/year of the next day. What I have isn't working(keep getting an error message). If someone could take a look and tell me why it wont work and/or point me in the right direction, that would be awesome.

Date Class:

Date Tester:

Output:
Today's date is 4/24/2012
My birthday is 1/5/2012
My brother's birthday is 7/20/2012
Christmas is on 12/25/2012
4/24/2012 equals 4/24/2012 true
4/24/2012 equals 12/25/2012 false
Is 2000 a leap year? true
Is 2001 a leap year? false
Is 1600 a leap year? true
Is 2012 a leap year? false
Exception in thread "main" java.lang.StackOverflowError
at Date.daysInMonth(Date.java:22)
at Date.nextDay(Date.java:31)<--This keeps repeating for quite a few more lines

Again if someone could help me out that would awesome! In the mean time I'll keep at it.

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your nextDay method you are recursively calling it- see return nextDay(). But I think your requirements is to return a new Date object. You have the date, month and year values calculated, instead of invoking nextDay() again you need to actually return a new Date object.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Richard wrote:Again if someone could help me out that would awesome! In the mean time I'll keep at it.

Mohamed's basically done that, but a little tip for you:
If you use 1-based months, don't use arrays - or if you do, document why, for example, your array of day lengths has a '0' as its first element (you and I both know why, but it's a non-standard use of an array).

My slight preference would be to keep both months and days 0-based internally - you'll find it makes calculations a lot easier - although there's nothing to stop you accepting 1-based values in your constructor.
If you really feel you must use 1-based values everywhere, what about something like:? Very clear, and no arrays needed.

HIH

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!