• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creation and usage of a class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm doing laundry! Look how clean this tiny ad is:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic