In: Computer Science
Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not.
The year is leap year if it is divisible by 4, but not divisible by 100 except if it is divisible by 400.
Examples
In the main method, test your code with above four years and another five years that will be generated randomly by the code. Use two separate for-loops. In the first for-loop in each of the four iterations user should be asked to provide one year and when you test the program you input the above four years. For each year program should report whether it is leap or non-leap. The second loop should have five iterations and at each iteration program should generate a random integer for year in the range 1582 and 2019 (including the limits) and report whether it is leap or non-leap.
//LeapYear.java import java.util.Random; import java.util.Scanner; public class LeapYear { public static boolean isLeap(int year){ if(year<1582){ return false; } else if(year % 4 == 0 && year % 100 != 0){ return true; } else if(year % 400 == 0){ return true; } return false; } public static void main(String args[]){ int yearNumber; Scanner scanner = new Scanner(System.in); for(int i = 0;i<4;i++) { System.out.println("Enter an year number: "); yearNumber = scanner.nextInt(); if (isLeap(yearNumber)) { System.out.println(yearNumber + " is a leap year"); } else { System.out.println(yearNumber + " is not a leap year"); } } Random random = new Random(); for(int i = 0;i<5;i++){ yearNumber = random.nextInt(437) + 1582; if (isLeap(yearNumber)) { System.out.println(yearNumber + " is a leap year"); } else { System.out.println(yearNumber + " is not a leap year"); } } } }