B543a43dc8cb0aae562278484db60d25

This *should* calculate leapyears

import java.util.Scanner;

public class LeapYear {

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);

		System.out.println("Please input a year");
		int input = in.nextInt();
		
		int remainder = (input % 4); 
		int century = (input % 100);
		
		if (remainder == 0) {
				if (century % 100 == 0) {
					System.out.println("The year: " + input + " is a leapyear");
				}
				else {
					System.out.println("The year: " + input + " is a leapyear");
				}
		}
		else {
			System.out.println("The year: " + input + " is NOT leapyear");
		}		
	}
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

john

October 16, 2008, October 16, 2008 18:37, permalink

No rating. Login to rate!

It's not correct though (for the Gregorian calendar). Years divisible by 400 are still leap years.

D41d8cd98f00b204e9800998ecf8427e

john

October 16, 2008, October 16, 2008 18:39, permalink

No rating. Login to rate!

Oh wait, you didn't do anything different for the cases of the if-else clause. Years divisible by 100 but not by 400 are not leap years.

1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

October 16, 2008, October 16, 2008 19:05, permalink

No rating. Login to rate!
import java.util.Scanner;

public class LeapYear {

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);

		System.out.println("Please input a year");
		int input = in.nextInt();
		
		if (input % 4 == 0 && (input % 100 != 0 || input % 400 == 0)) {
			System.out.println("The year: " + input + " is a leapyear");
		} else {
			System.out.println("The year: " + input + " is NOT leapyear");
		}
	}
}
F5580b2585a846f86b5c60be5310e0d3

Kaloyan

October 16, 2008, October 16, 2008 20:58, permalink

1 rating. Login to rate!

You do not need to implement the logic yourself. It is already part of the method "isLearYear" in java.util.GregorianCalendar

import java.util.Scanner;

public class LeapYear {

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);

		System.out.println("Please input a year");
		int input = in.nextInt();
		
		if (new GregorianCalendar().isLeapYear(input)) {
			System.out.println("The year: " + input + " is a leapyear");
		} else {
			System.out.println("The year: " + input + " is NOT leapyear");
		}
	}
}
10227e415d1c4e4e1cc719c6ccd70934

Debajit

October 17, 2008, October 17, 2008 20:08, permalink

1 rating. Login to rate!

First of all, you've mixed up the core logic of finding the leap year with I/O code (Functional abstraction). Using a separate function isLeap() also allows you to test that function independently using automated testing etc. The refactored code is as follows:

bool isLeap (int year)
{
    return (year % 4 == 0 && year % 100 != 0) 
           || (year % 400 == 0);
}

int main ()
{
    cout << "Enter year: ";
    int year;
    cin >> year;
    cout << "The year " << year << " is " << (isLeap(year) ? "" : "NOT") << " a leap year";
}
B543a43dc8cb0aae562278484db60d25

juo100@gmail.com

October 27, 2008, October 27, 2008 11:45, permalink

No rating. Login to rate!

Thanks for all the refactorings, I went with the below code in the end.

import java.util.Scanner;

public class LeapYear {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		
		int input = 0;
		System.out.println("Please input a year (enter -1 to stop)");

		while ( (input = in.nextInt()) != -1) {
			
			
			if (input % 4 == 0 && (input % 100 != 0 || input % 400 == 0)) {
				System.out.println("The year: " + input + " is a leapyear");
			} else {
				System.out.println("The year: " + input + " is NOT leapyear");
			}
			System.out.println("Please input a year");

		}
		System.out.println("Bye");
	}
}
D41d8cd98f00b204e9800998ecf8427e

Gice

September 27, 2009, September 27, 2009 22:56, permalink

No rating. Login to rate!

If you want no-one to see what you're doing you can go with this one

bool LY(int y){return y%4==0?y%100?true:y%400==0?true:false:false;}

Your refactoring





Format Copy from initial code

or Cancel