Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment Java-3 (Leap) Algorithm Logic

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question to ask, albeit a silly one.
After nearly blowing the fuse in my brain, I have sort of sussed out how to
do the leap year algorithm, well, the first bit of it.
The line of code below calculates if a year such as 1600 or 1700 is a leap
year:-
if (((num % 100) == 0 ) && ((num % 400) == 0))

But the thing is, I am having a problem with the other part of it, i.e.,
incorporating the ((num % 4)==0) bit, to do the check on whether a year is divisible by four.
I thought this would work, but the syntax is wrong:-
if (((num % 100) == 0 ) && ((num % 400) == 0)) || ((num % 4)==0)
and doing this:-
((((num % 100) == 0 ) && ((num % 400) == 0)) || ((num % 4)==0)) just
makes the logic of the algorithm completely wrong!!

I am seriously miffed as to how to combine them, using the correct bracket
logic.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve, have you read the Style Guide? I mean, like, all of it? Um, you know, all the way through the part on Self-Documenting Code?
It's really worth a peek at this point, believe me.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Pauline, she's just too damn cryptic...
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose I could put the exact section number...
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The algorithm in the style guide is wrong, so I have been told.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's news to me. Who told you that?
Either way, the style tips you get there, if applied to your code above, would make it a lot easier to check for logic (as opposed to fighting with parens and syntax).
Maybe a good starting place for correct "bracket logic" would be to reduce the amount of brackets (parentheses) that you need to deal with...
[ December 10, 2002: Message edited by: Pauline McNamara ]
 
David Mason
Ranch Hand
Posts: 56
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Jensen:
The algorithm in the style guide is wrong, so I have been told.

I tried the easy option on this one and "borrowed heavily" from the Style Guide. Even from such a start, it still took 3 attempts to get through. :roll: The logic is correct, but you can improve on the readability !
And the algorithm is presented in a number of styles, so choose wisely and you may get through more quickly.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The algorithm in the style guide is wrong, so I have been told.
Well, of the three examples shown, the first does not compile due to inconsistency over whether the variable is named "y" or "year". Once this is corrected, all three algorithms execute correctly for any possible argument. The only other "problems" with the methods are minor complaints:
(1) The methods should really be declared static since they make no use of any instance variables.
(2) The methods usually perform a few more calculations than necessary. From a performance-is-everything point of view, they could be reworked to be slightly faster, by assuming that ( year % 4 == 0 ) will be false more often than it's true, and that ( year % 400 == 0 ) will be false more often than true. It's possible to reorder the test so that in many cases only one is executed, and it's quite rare to require all three.
[Code deleted to protect the innocent]
This might be what someone meant when they said the algorithms shown were "wrong" - but they really should've said "a tiny bit inefficient". Practically speaking it doesn't matter much.
[ December 12, 2002: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic