This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Creation and usage of a class  RSS feed

 
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.

 
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.
 
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!