Spaces:
Running
Running
Create 9. Leap years, part 2 (two-part exercise)
Browse files
Week 2: Methods, strings and lists/9. Leap years, part 2 (two-part exercise)
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1. First, write the method
|
| 2 |
+
|
| 3 |
+
public static boolean isLeapYear(int year)
|
| 4 |
+
|
| 5 |
+
...which returns true or false depending on whether the given year is a leap year or not.
|
| 6 |
+
A year is a leap year if it is evenly divisible by four.
|
| 7 |
+
However, a year divided by 100 is a leap year only if it is also divided by 400.
|
| 8 |
+
So, for example, 2000 and 2004 are leap years, but 1900 is not.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
2. After that, write the method
|
| 13 |
+
|
| 14 |
+
public static void leapYears()
|
| 15 |
+
|
| 16 |
+
...which asks the user to enter the start and end of the year.
|
| 17 |
+
The method then prints all leap years found between these years.
|
| 18 |
+
Make use of the method isLeapYear!
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
Example on method execution:
|
| 22 |
+
Give start: 1970
|
| 23 |
+
Give end: 1977
|
| 24 |
+
1972 is a leap year
|
| 25 |
+
1976 is a leap year
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
import java.util.Random;
|
| 36 |
+
import java.util.Scanner;
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
public class Test{
|
| 41 |
+
public static void main(String[] args){
|
| 42 |
+
final Random r = new Random();
|
| 43 |
+
leapYears();
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
// ADDED 1
|
| 48 |
+
public static boolean isLeapYear(int year){
|
| 49 |
+
if (year % 4 == 0) {
|
| 50 |
+
// divided by 100
|
| 51 |
+
if (year % 100 == 0) {
|
| 52 |
+
// divided by 400
|
| 53 |
+
if (year % 400 == 0) {
|
| 54 |
+
return true;
|
| 55 |
+
}
|
| 56 |
+
return false;
|
| 57 |
+
}
|
| 58 |
+
// normal leap year, divided by 4
|
| 59 |
+
else {
|
| 60 |
+
return true;
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
// not a leap year
|
| 64 |
+
return false;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
// ADDED 2
|
| 69 |
+
public static void leapYears() {
|
| 70 |
+
Scanner reader= new Scanner(System.in);
|
| 71 |
+
|
| 72 |
+
System.out.print("Give start: ");
|
| 73 |
+
int start = Integer.valueOf(reader.nextLine());
|
| 74 |
+
System.out.print("Give end: ");
|
| 75 |
+
int end = Integer.valueOf(reader.nextLine());
|
| 76 |
+
|
| 77 |
+
for (int i = start; i <= end; i++) {
|
| 78 |
+
if (isLeapYear(i)) {
|
| 79 |
+
System.out.println(i + " is a leap year");
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
Testing method isLeapYear...
|
| 91 |
+
Testing with parameter 1984
|
| 92 |
+
Is a leap year: true
|
| 93 |
+
|
| 94 |
+
Testing with parameter 1985
|
| 95 |
+
Is a leap year: false
|
| 96 |
+
|
| 97 |
+
Testing with parameter 1900
|
| 98 |
+
Is a leap year: false
|
| 99 |
+
|
| 100 |
+
Testing with parameter 2000
|
| 101 |
+
Is a leap year: true
|
| 102 |
+
|
| 103 |
+
Testing with parameter 2004
|
| 104 |
+
Is a leap year: true
|
| 105 |
+
|
| 106 |
+
Testing with parameter 2003
|
| 107 |
+
Is a leap year: false
|
| 108 |
+
|
| 109 |
+
Testing method leapYears...
|
| 110 |
+
Testing with input 1970, 1990
|
| 111 |
+
Give start: 1970
|
| 112 |
+
Give end: 1990
|
| 113 |
+
1972 is a leap year
|
| 114 |
+
1976 is a leap year
|
| 115 |
+
1980 is a leap year
|
| 116 |
+
1984 is a leap year
|
| 117 |
+
1988 is a leap year
|
| 118 |
+
|
| 119 |
+
Testing with input 1780, 1801
|
| 120 |
+
Give start: 1780
|
| 121 |
+
Give end: 1801
|
| 122 |
+
1780 is a leap year
|
| 123 |
+
1784 is a leap year
|
| 124 |
+
1788 is a leap year
|
| 125 |
+
1792 is a leap year
|
| 126 |
+
1796 is a leap year
|
| 127 |
+
|
| 128 |
+
Testing with input 2001, 2015
|
| 129 |
+
Give start: 2001
|
| 130 |
+
Give end: 2015
|
| 131 |
+
2004 is a leap year
|
| 132 |
+
2008 is a leap year
|
| 133 |
+
2012 is a leap year
|
| 134 |
+
|
| 135 |
+
Testing with input 1897, 1905
|
| 136 |
+
Give start: 1897
|
| 137 |
+
Give end: 1905
|
| 138 |
+
1904 is a leap year
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|