/ Published in: Java
Expand |
Embed | Plain Text
public boolean isLeapYear(){ boolean leapYear; int divideByFour = year % 4; int divideBy100 = year % 100; int divideBy400 = year % 400; if (divideBy400 == 0){ leapYear = true; } else if (divideBy100 == 0){ leapYear = false; } else if (divideByFour == 0){ leapYear = true; } else { leapYear = false; } return leapYear; }
Comments
Subscribe to comments
You need to login to post a comment.

Here is what I use:
Why perform the mod by 100 and divide by 400 every time. Most years never need those mod operations performed. Then, why test for mod by 400 first, when seldom does this test need to be performed. Ditto mod by 100. For almost all years, the mod by 4 test must be performed, so let's do it first, and skip the other mod operations and conditional tests most of the time. Not only is it more efficient, this function is simpler source code, and probably simpler object code too.
I put both functions in a for loop from 1 to 3000 and got identical results.
Thanks for the alternate code. You're absolutely right about cutting out a lot of unneccessary stuff.
Thanks for helping out a n00b!