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 !
john
October 16, 2008, October 16, 2008 18:37, permalink
It's not correct though (for the Gregorian calendar). Years divisible by 400 are still leap years.
john
October 16, 2008, October 16, 2008 18:39, permalink
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.
Maciej Piechotka
October 16, 2008, October 16, 2008 19:05, permalink
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");
}
}
}
Kaloyan
October 16, 2008, October 16, 2008 20:58, permalink
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");
}
}
}
Debajit
October 17, 2008, October 17, 2008 20:08, permalink
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";
}
juo100@gmail.com
October 27, 2008, October 27, 2008 11:45, permalink
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");
}
}
Gice
September 27, 2009, September 27, 2009 22:56, permalink
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;}
This *should* calculate leapyears