In: Computer Science
Instructions JAVA PROGRAMMING
1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive).
2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked.
3. Take a year input from the user (should be between 1500 and 2019)
4. If the input year is a leap year, print that year is a leap year; otherwise, print that the year is not a leap year.
5. Point of thought: how will you handle centurial years, i.e., years ending in 00, e.g., 1900, 1800, 1700, 2000 etc.?
Goals
File/Project Naming
Your project/file should be named “Main.java”.
Test Your Program
Sample Output
Your output should look like this:
This year cannot be checked. Try again!
This year cannot be checked. Try again!
Yes, 2016 is a leap year!
Nope, 1900 is NOT a leap year!
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("What year do you want to test? (make sure it's
between 1500 and 2017): ");
int year = sc.nextInt();
if(year < 1500 || year > 2017)
System.out.println("This year cannot be checked. Try
again!");
else
{
if(year % 400 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else if(year % 100 != 0 && year % 4 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else
System.out.println("No, " + year + " is NOT a leap year!");
}
}
}
// Hit the thumbs up if you are fine with the answer. Happy Learning!