This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 15-100 RECITATION 2 - FALL 2007
In the Gregorian calendar (the one we use today), an amazing property exists. On any given year, the dates 4/4, 6/6, 8/8, 10/10 and 12/12 all occur on the same day of the week. Also, so do 5/9 and 9/5 as well as 7/11 and 11/7. In addition, so does the last day of February. These days are known as "doomsdays". Exercise Using Eclipse, write a simple Java program in a project named Recitation2 that contains a class named DoomsdayComputer with a main method that prints out the day of the week on which the doomsdays occur for a given input year between 20002099. First, use the Scanner class to read in a year entered at the keyboard (e.g. 2007). Be sure to print out an appropriate prompt before you read the input so the user knows what to enter. Read this year in as an int, not a String. Then, to compute the day of the week for the input year, follow these steps in your program: 1. Let y be the last two digits of the year. (HINT: Taking a number modulo 100 gives you the value of the last two digits. Do you see why?) 2. Let a be the integer quotient when you divide y by 12. 3. Let b be the integer remainder when you divide y by 12. 4. Take the remainder from the previous step and divide it by 4, keeping just the integer quotient as c. 5. Add up a, b, and c to form the value d. 6. Compute e, the day of the week for the doomsdays, by adding 2 to d and then dividing by 7, keeping the remainder only. (Print this value out for debugging.) 7. Translate the value e into a day name (0 = Sunday, 1 = Monday, etc.) and print out the day name without using the if or switch statements. Define a string with the value "SUNMONTUEWEDTHUFRISAT" and use the value of e to compute the starting index of the corresponding day. Then use a substring method on this string to extract out the day abbreviation and print it out. Check your answers by finding an appropriate calendar online and checking to see if your program computes the correct day for the doomsdays. Here's one website: http://www.timeanddate.com/calendar/ Advanced Exercise (If you have time...) Modify your program so that it prints out the day of the week that your birthday lands on for the given year, assuming it is not a leap year. (HINT: On paper, compute how many days your birthday is from one of the doomsdays. Then once you compute the day of the doomsdays, you can compute when your birthday is relative to the selected doomsday.) Example: Say your birthday is on April 10. A nearby doomsday is 4/4 (April 4). If you compute doomsday to land on a Tuesday (e=2), then April 10 is +6 days away from the 4/4 doomsday, so your birthday would land on Monday. NOTE: If your birthday is in January or February, the day of your birthday will be off by one if it is a leap year. (For example, if your program says your birthday is on a Monday and it's a leap year, your birthday is really on Sunday.) To find out more about the Doomsday computation, go to http://en.wikipedia.org/wiki/Doomsday_(weekday) When You're Done... Create a zip file of the project folder for this recitation and submit this zip file on Blackboard to the Recitation area. DO NOT ERASE YOUR RECITATION PROJECT FOLDER IN CASE WE NEED TO SEE IT LATER (IF THE SUBMISSION IS DONE INCORRECTLY). ...
View Full Document
This note was uploaded on 01/25/2010 for the course CS 15100 taught by Professor Tom during the Fall '07 term at Carnegie Mellon.
- Fall '07