CS 115 Homework 5
50 Points
Due by Friday February 26, midnight
Submit your program with the Canvas Link.
Educational goals of this lab - verify that every student can
- use a mod operator
- use if statements
- write a for loop
Preparations:
Watch these videos
(50 points) Individual Problem: Leap Year Detection
- Leap years are important to our calendar system. Without them the
calendar year would "drift" through the seasons.
They are necessary because a true "solar year", the time taken for the Earth
to go around the sun and get back to the same place in its orbit is not
exactly 365 days. It's about 365 and a quarter days. So an extra day
is added in every 4 years (easier to do one whole day than to make people
add a quarter day every year!). For a long time this fix worked.
But eventually they realized that January was in the springtime!
The problem is that the solar year is not exactly 365 and a quarter days.
It's a tiny bit less than that. They added one other fix (in 1581);
they "took back" a few leap days. Every year that is a "century" like
1600, 1700, 1800, etc. would only be a leap year if it also divided
evenly by 400. So 1600, 2000, 2400, 2800 will be (or were) leap years.
1900, 2100, 2500 are (were) not.
- Rules: A year is a leap year if it is evenly divisible
by 4, unless it is divisible by 100, in which case it must also
be divisible by 400.
- Examples: 1996 is a leap year because it is
divisible by 4 and not 100. 1997 is not a leap year because it is
not divisible by 4. 2000 is a leap year because it is divisible by
100 and 400. 2100 will not be a leap year because it is divisible by
100 but not 400.
This venn diagram may help.
The pink and blue areas are the leap years. The largest circle represents
ALL positive integers.
Your program will ask the user for a range of years to check for
leap years and print out all leapyears in that range. The range should be
inclusive, including both endpoints given.
If the numbers are given in reverse order, your program can reverse them.
Sample run
Leap Year Detector
What is the start of the range? 1999
What is the end of the range? 2015
Leap Years in range 1999 to 2015
2000
2004
2008
2012
Sample run
Leap Year Detector
What is the start of the range? 1900
What is the end of the range? 2020
Leap Years in range 1900 to 2020
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012
2016
2020
Sample run (with reversed start and stop values)
Leap Year Detector
What is the start of the range? 1975
What is the end of the range? 1950
Leap Years in range 1950 to 1975
1952
1956
1960
1964
1968
1972
(12 points) Test Plan
- Any integer can be a test case input. You need to make sure you have
tested some numbers in each of the categories, "not divisible by 4",
"divisible by 4 but not 100", etc. Go to
this web page
for a leap year calculator to check the answers you worked out.
- Give a list of 4 test cases in a comment at the bottom of your program.
They should have all the information in a test plan, as we have used all
semester. So you have to provide the description of what you are testing, input to test that, expected output for each test case.
(12 points) Design
- Write the usual 3 P's for your prolog.
- Describe what your control structures (loops and if's) are doing.
A comment for each loop and if is sufficient.
(26 points) Implementation
- Write a program which will ask the user for start and stop values of
the range of years they want checked for leap years.
- If the start value is greater than the stop value, swap the values of
the variables.
- Use a for loop for that range.
- Make sure the loop runs through the entire range including the last one
given by the user.
- The logic for determining if a number is a leap year can be written
with nested if's or with boolean operators (and, or, not). Either way is
ok.
- Hint: the mod operator is required here.
Submit your finished program with the Canvas link.