Check if a given year is a leap year

The Problem

Create a program that inputs a year from the user, finds out if it's a leap year and presents the result to the user.

Discussion

Of course when we are talking about dividing years, we mean the year number in the Gregorian calendar. As we know, a leap year is divisible by 4. In addition, if it ends with 00, it must be divisible by 400.

We can check if a number is divisible by another number, by using the modulus operator %. The modulus operator returns as a result the reminder of the division of the first operand by the second operand:

boolean isDivisibleBy4 = theYear % 4 == 0;

It is easy to find if a number ends with 00. If the number is divisible by 100 than it ends with 00 and vice verse:

boolean isEndingWith00 = theYear % 100 == 0;

Further we can notice that if the year number is divisible by 100 than we can check if it is divisible by 400 by first dividing it by 100 and than checking if the result is divisible by 4. Thus we can simplify the check:

int checkValue = theYear;
boolean isEndingWith00 = (theYear % 100) == 0;
if (isEndingWith00) {
   checkValue = theYear / 100;
}
boolean isLeap = (checkValue % 4) == 0;

Ore using the ternary conditional operator ?: we can rewrite the above check:

boolean isEndingWith00 = (theYear %100 == 0);
int checkValue = (isEndingWith00 ? theYear / 100 : theYear);
boolean isLeap = (checkValue % 4) == 0;

For educational purposes we can encapsulate the logic in a class Year. The class will have one attribute - yearNumber and one method isLeapYear.

The Solution

Here is a sample implementation of the Year object that solves the problem of checking if a given year is a leap year.

/**
 * Class that represents a Year in the Gregorian calendar.
 *
 * @author Ivan Georgiev
 */
public class Year {
	private int theYearNumber;
 
	public Year(int theYearNumber) {
		this.theYearNumber = theYearNumber;
	}
 
	public int getYearNumber() {
		return theYearNumber;
	}
 
	public boolean isLeapYear() {
		boolean isEndingWith00 = (theYearNumber % 100) == 0;
		int testValue = (isEndingWith00) ? theYearNumber / 100 : theYearNumber;
		boolean result = (testValue % 4) == 0;
		return result;
	}
}

And here is a sample Java application that uses showInputDialog and showMessageDialog to interact with the user. We have also added error checking.

/**
 * @(#)IsLeapYear.java
 *
 * Sample Application for checking if a given year is a leap year.
 *
 * @author Ivan Georgiev
 * @version 1.00 2008/9/2
 */
 
import javax.swing.*;
 
public class IsLeapYear {
 
    public static void main(String[] args) {
    	String yearInput;
    	int yearNumber;
    	Year yearObject;
    	String feedbackMessage;
 
 		while (true) {
	  		yearInput = JOptionPane.showInputDialog(
 					null, 
 					"Enter a year to check if it is a leap year:");
	 		// JOptionPane.showInputDialog returns null if the 
	 		// user clicks the Cancel button.
 			if (yearInput == null) break;
	 		try {
	 			yearNumber = Integer.parseInt(yearInput);
	 		} catch (NumberFormatException nfe) {
	 			JOptionPane.showMessageDialog(
	 					null,
	 					"You must enter a valid integer number.");
	 			continue;
	 		}
	 		yearObject = new Year(yearNumber);
	 		feedbackMessage = "The year is ";
	 		if ( ! yearObject.isLeapYear()) {
	 			feedbackMessage += " not ";
	 		}
	 		feedbackMessage += "a leap year.";
	 		JOptionPane.showMessageDialog(null, feedbackMessage);
 		} while (yearInput != null );
    }
}

Back to Programming Problems in Java, Home, resume writing service, computer science project

 
programming/problems/java/leapyears.txt · Last modified: 2012/01/31 12:50 by piternoize
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki