• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logic Problem Leap Year Method  RSS feed

 
Anna Grey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method is driving me crazy. It returns day for all cases of Feb. 29th, when it should only return day if it is a leap year, if it is not a leap year 1 should be returned. It works fine for everything except Feb 29th.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you already know month is 2 and day is 29 , I guess it already denotes it's a leap year. So what is the significance of next if?

Correct me if I am wrong
 
Anna Grey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my constructors call this method:


And then it is later called by the toString:
public String toString()


If for example I do: in my test class then the toString prints 2/29/2013 when it should print 2/1/2013. It works for everything else, if for instance I do it will print as 4/1/2013.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna - when checkDay() is called, what are the values of month and year? I suggest you put a println() statement inside the checkDay() method to verify these values. You may well learn something important.
 
Anna Grey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I added print statements.
This is my test class.

this is the output:returned day
The initail date is: 2/29/2013
Date with new value is: 11/1/2003
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but what are the values of MONTH and YEAR, at the time checkDay() is called? What happens if you put print statements for those values, inside the checkDay() method? Say, at the very beginning of that method.
 
Ulf Dittmer
Rancher
Posts: 42972
73
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna Torjkov, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/81339-need-help-leap-year-method.html
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but at that thread, she hasn't yet posted the part of the code that actually causes the problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna Torjkov wrote:This method is driving me crazy...

Anna,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Oh, and you're usually better off using spaces for indentation, rather than TABs.

Thanks.

Winston
 
Anna Grey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't know it was against the rules to post on other sites, I was trying to get an answer quickly and never really found one.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you're still working on it, I strongly suggest looking at my second post above (which is just restating the first one really). Because that will lead you directly to the source of your problems.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Well if you're still working on it, I strongly suggest looking at my second post above (which is just restating the first one really). Because that will lead you directly to the source of your problems.

@Anna: And if I may, I'd suggest passing ALL the fields to your checkDay() method. It'll make it much less tightly coupled.

However, if you really want to simplify this, back up and work out the actual problems involved in date validation (Ulf's link pointed you to one). And there are precisely two:
1. Is the year a leap year?
2. How many days are there in the month? (which requires the answer to #1 if, and ONLY if, the month is 2).
Once you have that information, validating the day is a piece of cake.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!