The current calendar, called the Gregorian calendar, was introduced in 1582. Every year divisible by four was declared to be a leap year, with the exception of the years ending in 00 (that is, those divisible by 100) and not divisible by 400. For instance, the years 1600 and 2000 are leap years, but 1700, 1800, and 1900 are not.
Write a program that requests a range of years as input and displays that year to the screen if it is a leap year, as well as list of years that are "century years" (those divisible by 100) but are NOT leap yearsPlease follow this specification exactly:
the first message to the user to prompt for the first of the two inputs is:
"please enter a start year for the leap year range calculator"
the second message to the user to prompt for the second of the two inputs is:
"please enter an end year for the leap year range calculator"
the display for the date range needs to be in this format:
"The following years are leap years:"
1596
1600
1604
1608
the display for the list of years that fall within the year range that are NOT leap years needs to be in this format:
"The following century years that fall within the date range and are NOT leap years are:"
1500, 1700, 1800
if there are no century years that are not leap years to be displayed, the following message needs to be displayed instead:
"there are no century years that are not leap years within the range you have specified".
NOTE: there are two sections to display: the list of leap years within the specified year range with the years listed vertically (down the screen) and a list of century years within the specified year range that are NOT leap years, listed horizontally (across the screen).
During the recent exam, many people were strugging with this question. This blog entry will cover the reading of a specification and how to interpret it to start to solve the problem and provide a solution.
Later blog entries will explain further how to come up with the pseudocode to then create the program.
Here is one way of interpreting the specification to get going.
1: Read carefully to understand what is being asked: don't assume.
The question is an adaptation (modification) of one of the questions from the textbook. Some students assumed it was exactly the same question and simply submitted their class work as a solution: sorry, but it wasn't the same question so that wasn't going to be the answer.
2: Look for inputs and outputs.
The question says that there are two inputs and even provides the text (the words) to use to display a message to the user to ask for their input.
the first message to the user to prompt for the first of the two inputs is:
"please enter a start year for the leap year range calculator"
the second message to the user to prompt for the second of the two
inputs is:
"please enter an end year for the leap year range calculator"
So, since this is a Console application, that means two prompts (Console.WriteLine()) and two inputs from the user (Console.ReadlLine()).
What the specification also says is that the input is "a range of years" and mentions a "start year" and an "end year". This is important to what is required for processing: a range is (in this case) a continous group of years starting from the "start year" and finishing at the "end year". This could mean some processing within a loop, starting from the "start year" and finishing at the "end year". NOTE: condition to end exactly on the "end year" (and not the year before) is implied because it didn't mention anything about it finishing the year before the end year (but more about this loop in a later blog post).
There are two sections to display but three messages, so this should alert you to the need that there is an "IF" statement needed somewhere in the output, and that may influence the processing needed for the solution.
NOTE: there are two sections to display: the list of leap years within the
specified year range with the years listed vertically (down the screen) and a
list of century years within the specified year range that are NOT leap years,
listed horizontally (across the screen).
The first section has the format provided for you to make sure your program does what the specification asks:
"The following years are leap years:"
1596
1600
1604
1608
which also says what the text to display should say.
The specification states that the second section has a particular display as well:
the display for the list of years that fall within the year range that are NOT
leap years needs to be in this format:
"The following century years that
fall within the date range and are NOT leap years are:"
1500, 1700, 1800
and it says that there is an alternative display under certain conditions (which means an "IF" statement based on some condition):
if there are no century years that are not leap years to be displayed, the
following message needs to be displayed instead:
"there are no century
years that are not leap years within the range you have specified".
3: Look for the "Meat in the sandwich"
The expression "Meat in the sandwich" means "the tasty part" or main section of the processing, which is the logic to work out which years are leap years and which ones are not. There are leap years to be found, and "century years" that are not leap years.
4: Put all the bits together
So, by carefully reading the specification, you can find out the following things to start to come up with a solution:
- There are two prompts to the user, and two pieces of data to use ("start year" and "end year").
- There are leap years to be found, and "centuryYears" that are not leap years.
- There is logic provided to work out what is a leap year.
- there is a range of years, which could mean a loop with processing within it. The loop will go from "startYear" to "endYear" continously, checking each year to see if it is what is needed for the output.
- there are two sections as part of the output, which means two different processing of the range of years (or at least two different parts of processing, perhaps with an "IF" within the loop)
- there is some "either this one or that one" to do with the second part of the display ("there are no century years ...")
and that should be enough to get going with the pseudocode.