Difference between revisions of "Leap Year Problem"

From CompSciWiki
Jump to: navigation, search
m (Removing of <pre> tags from SolutionCode. Replaced | character with pipe template.)
(EDIT: Cleaned up the description of what qualifies as a leap year. It still could be better though)
Line 3: Line 3:
 
|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.<BR /><BR />
 
|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.<BR /><BR />
  
Here are the criteria that dictates whether or not a year is a leap year: If a year is evenly divisible by 4, then it is a leap year -- unless it is also divisible by 100, in which case it is not.  An exception to this is if the year is divisible by 400, then it is still a leap year.<BR /><BR />
+
A year is a leap year only if it is evenly divisible by 4 and not evenly divisible by 100, or if the year is evenly divisible by 400.
 +
<BR /><BR />
  
 
Examples:<BR>
 
Examples:<BR>

Revision as of 01:56, 9 April 2010

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 year is a leap year only if it is evenly divisible by 4 and not evenly divisible by 100, or if the year is evenly divisible by 400.

Examples:
1996 was a leap year because it was divisible by 4.
1900 was not a leap year in spite of being divisible by 4, because it is also divisible by 100.
2000 was a leap year because it is divisible by 400.
2009 was not a leap year, because it is not divisible by 4.

 

By Students...

My by students section will go here!

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 for you by now. Continuously asking the user for more years for input should simply be a matter of surrounding your code so far with a while loop that exits whenever the input returned by your 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