202k views
3 votes
Write a program PrintLeapYear.java that prints a table of leap years.

 Define a method call isLeapYear that will take any year as parameter, then return true if
that year is leap year and return false if the year is not leap year. Leap year is every 4
years but not every 100 years, then again every 400 years.
 Program will take user input for the start year (integer)
 Print out the leap year table from the start year going forward 120 years using control
structure and the isLeapYear method

1 Answer

3 votes

Let's break down the task and build the Java program step by step.

1. First, we need to create a method called `isLeapYear` that takes a year as a parameter and returns true if the year is a leap year and false otherwise. We need to check if the year is divisible by 4 but not divisible by 100, or if the year is divisible by 400. This logic is based on the rule that a year is a leap year if it is evenly divisible by 4, except for years that are divisible by 100. However, years divisible by 400 are also leap years.

2. Next, we need to take an input from the user for the start year.

3. We will then use a loop to print the leap years for the next 120 years, starting from the input year.

Here is the Java program:

```

import java.util.Scanner;

public class PrintLeapYear {

public static void main(String[] args) {

// Step 2: Take user input for start year

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the start year: ");

int startYear = scanner.nextInt();

// Step 3: Print the leap year table

System.out.println("Leap years from " + startYear + " to " + (startYear + 120) + ":");

for (int year = startYear; year <= startYear + 120; year++) {

// Use the isLeapYear method

if (isLeapYear(year)) {

System.out.println(year);

}

}

}

// Step 1: Define isLeapYear method

public static boolean isLeapYear(int year) (year % 400 == 0);

}

```

Let me explain what each part of the code does:

1. `import java.util.Scanner;` - We import the Scanner class which we will use to get input from the user.

2. `public class PrintLeapYear { ... }` - Defines a class named `PrintLeapYear`.

3. Inside the main method:

- We create a Scanner object to read the input from the user.

- We ask the user to enter the start year and store it in the variable `startYear`.

- We print a header for the leap year table.

- We use a for loop to iterate through each year from the start year to 120 years ahead. Inside the loop, we use the `isLeapYear` method to check if the year is a leap year. If it is, we print the year.

4. The `isLeapYear` method takes an integer `year` as a parameter and returns true if it's a leap year based on the conditions described above (divisible by 4, not divisible by 100 unless divisible by 400).

To run the program, save it as `PrintLeapYear.java`, and then compile it using the command `javac PrintLeapYear.java`. After compiling, you can run the program with the command `java PrintLeapYear`, and it will ask you to enter the start year and then print the leap years for the next 120 years.

User Serif
by
7.6k points