![]() |
CPSC 110-08: Computing on Mobile Phones
|
2012 is a Leap Year. But what exactly is a leap year and what years are leap years?
In this homework (and follow-up in class assignment) we will look at the definition of a leap year and turn it into an algorithm to test whether any given year is a leap year or not.
A leap year consists of 366 (rather than 365) days, with the extra day coming on February 29. The purpose of the extra day is to bring the calendar more in line with the time it takes the earth to make a full orbit around the sun, which takes 365.242199 days. The leap year adjustment occurs approximately (although not exactly) every 4th year.
A closer approximation is provided by the following rule, which was adopted as part of the Gregorian Calendar:
Leap Year Definition. A year is a leap year if it is evenly divisible by 4 but not evenly divisible by 100 unless it is also evenly divisible by 400. So 1996 was a leap year. But 1900 was not a leap year because, although it is divisible by 4, it is also divisible by 100 and not by 400. 2000 is a leap year because it is divisible by 400.
Here are some interesting facts about leap years from timeanddate.com:
[During a Leap Year] the Ladyes have the sole privilege, during the time it continueth, of making love unto the men, which they may doe either by wordes or lookes, as unto them it seemeth proper; and moreover, no man will be entitled to the benefit of Clergy who dothe refuse to accept the offers of a ladye, or who dothe in any wise treate her proposal withe slight or contumely.
There was even a 2010 movie with this theme. Was 2010 a leap year?
Let's build an app to figure out if a a year entered by the user is a Leap Year.
The app contains a Label with the text "Leap Year?", a TextBox where the user will enter a year, a Button that the user will click to test whether the year is a leap year, and a Notifier that will display the answer.
As you can see, when Button1 is clicked, the app takes the text from TextBox1 and tests whether it isLeapYear or not. It then uses a Notifier component to report the result.
The isLeapYear block here is only partially defined. We'll complete it in class. It is an example of a procedureWithResult block -- also known as a programmer-defined function. You can find it in the Built-in > Definition drawer.
Functions: You've been using lots of built-in functions in your apps -- e.g., Math functions like random integer and Text functions like join and make text. In this app we'll be writing our own functions to simplify our algorithm. The isLeapYear function is an example of a boolean function -- i.e., one that returns either true or false. This lets us use it in the Test slot of an if/else block.
# Definition of leap year. if year is divisible by 4: then if year is divisible by 100: then if year is divisible by 400: set isALeapYear to true # Divisible by 4 and 100 and 400 else set isALeapYear to false # Divisible by 4 and 100 but not 400 else set isALeapYear to true # Divisible by 4 but not by 100 else set isALeapYear to false # Not divisible by 4
Notice in this algorithm that one if/else statement (block) is contained (nested) inside another. How many if/else blocks are involved?
The TAs are available on Sunday, Tuesday, and Thursday evenings in MCEC 136. See the syllabus for their hours.
We'll finish the LeapYear app.