Leap Year Problem

From CompSciWiki
Revision as of 15:22, 8 December 2011 by RyanV (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Write a short program that takes a year as input from the user, and then determines whether or not that particular year is (or was) a leap year. Your program should keep asking for more years until the user enters something non-numeric, or clicks 'Cancel' on the input box.

A given year is a leap year if it is evenly divisible by 4 and not evenly divisible by 100. However, if the year is evenly divisible by 400, then it is a leap year (in spite of being divisible by 4 and 100).

Examples:
1996 was a leap year because it was divisible by 4 (but not by 100).
1900 was not a leap year because it is divisible by 100, and is not divisible by 400.
2000 was a leap year because it is divisible by 400.
2009 was not a leap year, because it is not divisible by 4 at all.

 

...By Students

"Even though the Leap Year Problem is a classic problem for novice programmers (I remember doing it back in my 101 class, too), leap year calculations can still be tricky! Even veteran programmers get it wrong from time to time -- just ask Sony and Microsoft, who both have products (the PS3 and the Zune, respectively) that have experienced widespread failure due to faulty leap year calculations.

So don't feel bad if you don't get this question quite right the first time. Don't let it be an excuse to not debug your programs before calling it done, though!"

Solution

There are four main parts to this problem. First, we need to get input from the user. Then we need to figure out if the year given by the user is a leap year. Once this is done, we need to inform the user of the result. Finally, we must repeat the process until the user decides to cancel, or enters something invalid.

By now you should be a pro at using JOptionPane's input dialog, so this shouldn't be too tricky -- just remember to parse the String input to an int, using the Integer.ParseInt method. Also, keep in mind that the input dialog will return a value of null if the user clicked 'Cancel' instead of entering a value (we will use this in the loop discussed later on).

Next, we need to determine if the year we've obtained from the user is a leap year. The trick here is to use Java's modulus operator (%) to determine whether the year is evenly divisible by 4, 100, or 400. The modulus operator gives us the remainder of the division of the two operands, so a result of 0 indicates that it is evenly divisible, while any other result indicates that it is not. Be careful while structuring your if statement matching the criteria for a leap year -- remember to use parentheses to keep your meaning clear when using logic operators!

Once you have your if structure set up, outputting the result using JOptionPane's message dialog should be straightforward. Surrounding your input, calculation and output routines with a while loop will allow for multiple input. It should exit when the input returned by the input dialog is null (ie, the user canceled). Exiting on invalid input shouldn't be a problem, as the Integer.ParseInt method will throw an exception if the string isn't an integer. While this may not be a clean way to exit, it will do until you learn advanced error-handling techniques :)

Code

Solution Code

Back to the Program-A-Day homepage