I am following "The Art and Science of Java" book and it shows how to calculate a leap year. The book uses ACM Java Task Force's library.
Here is the code the books uses:
import acm.program.*;
public class LeapYear extends ConsoleProgram {
public void run()
{
println("This program calculates leap year.");
int year = readInt("Enter the year: ");
boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
if (isLeapYear)
{
println(year + " is a leap year.");
} else
println(year + " is not a leap year.");
}
}
Now, this is how I calculated the leap year.
import acm.program.*;
public class LeapYear extends ConsoleProgram {
public void run()
{
println("This program calculates leap year.");
int year = readInt("Enter the year: ");
if ((year % 4 == 0) && year % 100 != 0)
{
println(year + " is a leap year.");
}
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
{
println(year + " is a leap year.");
}
else
{
println(year + " is not a leap year.");
}
}
}
Is there anything wrong with my code or should i use the one provided by the book ?
EDIT :: Both of the above code works fine, What i want to ask is which code is the best way to calculate the leap year.
They look the same to me, though note that this line in your code has some redundancy:could be replaced by:
If a number is a multiple of 400 then it's automatically also a multiple of 100 and 4.
edit: (7 years later!)
Please note that the above assumes the presence of the preceding
if ((year % 4 == 0) && year % 100 != 0)
from the original question!cletus's answer should be the accepted one: https://stackoverflow.com/a/1021373/8331
(I'd delete my own answer, but I can't since it's the accepted on)
As wikipedia states algorithm for the leap year should be
Here is a sample program how to check for leap year.
Your code, as it is, without an additional class, does not appear to work for universal java. Here is a simplified version that works anywhere, leaning more towards your code.
Your code, in context, works just as well, but note that book code always works, and is tested thoroughly. Not to say yours isn't. :)
It's almost always wrong to have repetition in software. In any engineering discipline, form should follow function, and you have three branches for something which has two possible paths - it's either a leap year or not.
The mechanism which has the test on one line doesn't have that issue, but generally it would be better to separate the test into a function which takes an int representing a year and returns a boolean representing whether or not the year is a leap year. That way you can do something with it other that print to standard output on the console, and can more easily test it.
In code which is known to exceed its performance budget, it's usual to arrange the tests so that they are not redundant and perform the tests in an order which returns early. The wikipedia example does this - for most years you have to calculate modulo 400,100 and 4, but for a few you only need modulo 400 or 400 and 100. This is a small optimisation in terms of performance ( at best, only one in a hundred inputs are effected ), but it also means the code has less repetition, and there's less for the programmer to type in.
From JAVA's GregorianCalendar sourcecode:
Where changeYear is the year the Julian Calendar becomes the Gregorian Calendar (1582).
In the Gregorian Calendar documentation you can found more information about it.
You can ask the GregorianCalendar class for this: