Lab 3: Loops

In this lab, you will practice writing loops, conditional statements, and boolean and mathematical operators.

A Calendar Class

Create a class called Calendar. The class should have a single static method called dayOfWeek that takes three int parameters, day, month, and year, and the method should return a String. The input will correspond to some date in the past or future, and the output will be the day of the week that date falls on.

Here is one way to write the method. We know that January 1, 2008 was a Tuesday. So our method will first compute the number of days between January 1, 2008 and the date given by the parameters. You will want a local variable that keeps count of this number of days. The number of days will be positive if counting forward and negative if counting backward. Start this counter at 0.

Now you have the number of days between January 1, 2008 and the date entered. You can find the day of the week by doing a % 7. If counting forward, the result will be between 0 and 6 corresponding to Tuesday through Monday. If counting backward, the result will be between 0 and -6 corresponding to Tuesday through Wednesday (i.e. in reverse). You can use this to return the String containing the appropriate day of the week.

Part 2 (Optional)

If you have completed above, add a check for illegal dates. If the month entered is smaller than 1 or larger than 12, or if the day entered is smaller than 1 or larger than the number of days in that month, return "Illegal Date". Also, we are only handling Gregorian dates. So if the date is earlier than September 14, 1752 return "Illegal Date".

Part 3 (Optional)

If you have completed above and want some fun, try adding in the Julian dates. In this case, the date before September 14, 1752 is September 2, 1752, and the leap year rule prior to this date is whether the year is divisible by 4.

When you are complete, email your class file to me.