File size: 2,765 Bytes
f2e8436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
1. First, write the method

public static boolean isLeapYear(int year)

...which returns true or false depending on whether the given year is a leap year or not.
A year is a leap year if it is evenly divisible by four. 
However, a year divided by 100 is a leap year only if it is also divided by 400. 
So, for example, 2000 and 2004 are leap years, but 1900 is not.



2. After that, write the method

public static void leapYears()

...which asks the user to enter the start and end of the year. 
The method then prints all leap years found between these years. 
Make use of the method isLeapYear!


Example on method execution:
Give start: 1970
Give end: 1977
1972 is a leap year
1976 is a leap year









import java.util.Random;
import java.util.Scanner;



public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        leapYears();
    }


    // ADDED 1
    public static boolean isLeapYear(int year){
        if (year % 4 == 0) {
            // divided by 100
            if (year % 100 == 0) {
                // divided by 400
                if (year % 400 == 0) {
                    return true;
                }
                return false;
            }
            // normal leap year, divided by 4
            else {
                return true;
            }
        }
        // not a leap year
        return false;
    }
    

    // ADDED 2
    public static void leapYears() {
        Scanner reader= new Scanner(System.in);

        System.out.print("Give start: ");
        int start = Integer.valueOf(reader.nextLine());
        System.out.print("Give end: ");
        int end = Integer.valueOf(reader.nextLine());

        for (int i = start; i <= end; i++) {
            if (isLeapYear(i)) {
                System.out.println(i + " is a leap year");
            }
        }
    }

}





Testing method isLeapYear...
Testing with parameter 1984
Is a leap year: true

Testing with parameter 1985
Is a leap year: false

Testing with parameter 1900
Is a leap year: false

Testing with parameter 2000
Is a leap year: true

Testing with parameter 2004
Is a leap year: true

Testing with parameter 2003
Is a leap year: false

Testing method leapYears...
Testing with input 1970, 1990
Give start: 1970
Give end: 1990
1972 is a leap year
1976 is a leap year
1980 is a leap year
1984 is a leap year
1988 is a leap year

Testing with input 1780, 1801
Give start: 1780
Give end: 1801
1780 is a leap year
1784 is a leap year
1788 is a leap year
1792 is a leap year
1796 is a leap year

Testing with input 2001, 2015
Give start: 2001
Give end: 2015
2004 is a leap year
2008 is a leap year
2012 is a leap year

Testing with input 1897, 1905
Give start: 1897
Give end: 1905
1904 is a leap year