This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Assignment Java-3 (Leap) Algorithm Logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Assignment Java-3 (Leap) Algorithm Logic" Watch "Assignment Java-3 (Leap) Algorithm Logic" New topic
Author

Assignment Java-3 (Leap) Algorithm Logic

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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.


John Bonham was stronger, but Keith Moon was faster.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Mar 25, 2001
Posts: 1746
    
    2
That Pauline, she's just too damn cryptic...
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I suppose I could put the exact section number...
Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
The algorithm in the style guide is wrong, so I have been told.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Jan 21, 2002
Posts: 56

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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment Java-3 (Leap) Algorithm Logic
 
Similar Threads
Assignment Java-3 (Leap)
Leap year problem explain pls??
1.3 Leap
whats wrong with this code?? number of days in month..
java-3 (leap)