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:
-
Let y be the last two digits of the year. (How do you compute
this using the modulo (%) operator?)
-
Let a be the integer quotient when you divide y by 12.
-
Let b be the integer remainder when you divide y by 12.
-
Take the remainder from the previous step and divide it by 4, keeping just
the integer quotient as c.
-
Add up a, b, and c to form the value d.
-
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?)
-
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/