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.
- First deal with the years. For each year between 2008 and the parameter entered, add 365 (if going forward) or
subtract 365 (if going backward) unless the year is a leap
year (divisibly by 4, not divisible by 100 unless it is divisible by 400) in which case you add or subtract 366.
You can determine if a year is a leap year using the % operator in an if statement.
The main trick is to deal with the fact
that you may have to count forward or backward. One simple way is to have two loops, one for forward and one for backwards
and use an if statement to decide which to use. There also is a way to do it with one loop.
- Now you know how many days between January 1, 2008 and January 1 of the parameter year. The next thing to handle is
the month. For each month between January (month 1) and the month entered, add the appropriate number of days. Remember
that April, June, September, and November have 30 days, February has 28 or 29 (depending on whether it is a leap year), and
the rest have 31.
- Finally, add in the number of days between the first day of the month and the day entered.
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.