15-110 FALL 2009 [CORTINA]

LAB 2: Using Scanner and String classes

In the Gregorian calendar (the one we use today), an amazing property exists. On any given year, the dates Apr 4 (i.e. 4/4), June 6 (6/6), August 8 ( 8/8), October 10 (10/10) and December 12 (12/12) all occur on the same day of the week. Also, so do May 9 (5/9) and September 5 (9/5) as well as July 11 (7/11) and November 7 (11/7). In addition, so does the last day of February. Also January 3 (1/3) on non leap-years and January 4 (1/4) on leap years. These days are known as "doomsdays".

EXERCISES

  1. Using Eclipse, write a simple Java program in a project named Lab2 that contains a class named DoomsdayComputer with a single main method that prints out the day of the week on which the doomsdays occur for a given input year between 2000-2099.

    First, use the Scanner class to read in a year entered at the keyboard (e.g. 2009). 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 of the doomsdays for the input year, follow these steps in your program:

    1. Let y be the last two digits of the year. (How do you compute this using the modulo (%) operator?)
    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, representing the day of the week for the doomsdays, by adding 2 to d and then dividing by 7, keeping the remainder only. Note that e should be a value between 0 and 6. (Why?)
    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/

  2. Modify your program so that it prints out the day of the week that Veteran's Day (Nov 11) occurs on for the given year. (HINT: On paper, compute how many days Veteran's Day is from one of the doomsdays. Then once you compute the day of the doomsdays, you can compute when Veteran's Day is relative to the selected doomsday by updating e by adding or subtracting from e. NOTE: Be careful... make sure e stays in the range 0 to 6.)

    Once you've done Veteran's Day, try Flag Day (6/14) and St. Patrick's Day (3/17). To find out more about the Doomsday computation, go to

    http://en.wikipedia.org/wiki/Doomsday_(weekday)

HANDIN

At the end of lab, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1.