In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”.
I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment.
II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class.
III. Write one static class method (isLeapYear). The method isLeapYear should take one integer argument and will return a boolean. The method should compare the input (a year) with the qualifications for determining whether a given year is a leap year or not, and return the appropriate true or false value on completing these checks. The rule for leap years is as follows: if the year is evenly divisible by 4, it is a leap year, except in the case where it is also evenly divisible by 100 but not evenly divisible by 400. Several example method calls appear below.
LeapYear.isLeapYear(2016) // returns true, divisible evenly by 4
LeapYear.isLeapYear(2015) // returns false, not divisible evenly by 4
LeapYear.isLeapYear(1900) // returns false,
LeapYear.isLeapYear(2000) // returns true, divisible evenly by 4 and 100, but also by 400
IV. Complete the definition for main. Your program should create a new Scanner object, prompt the user to type in a year, and then collect an integer. The main method should then call the static method isLeapYear in order to determine whether or not the year in question fits the characteristics of a leap year. From this point, the results of the method call should be used to print a statement that expresses the results. For example, if the user enters 2015 as input: “The year 2015 is not a leap year.”
import java.util.Scanner; public class LeapYear { public static boolean isLeapYear(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an year: "); int year = in.nextInt(); if (isLeapYear(year)) { System.out.println("The year " + year + " is a leap year."); } else { System.out.println("The year " + year + " is not a leap year."); } } }
Get Answers For Free
Most questions answered within 1 hours.