Mimir Project

Login to class.mimir.io. Open Lab1: DayOfWeek project. Instructions are given below for reference.

How to determine the day of the week, given the month, day and year

First a brief explanation: In the Gregorian Calendar, over a period of four hundred years, there are 97 leap years and 303 normal years. Each normal year, the day of January 1 advances by one; for each leap year it advances by two.

 

 

As a result, January 1 year N occurs on the same day of the week as January 1 year N + 400. Because the leap year pattern also recurs with a four hundred year cycle, a simple table of four hundred elements, and single modulus, suffices to determine the day of the week (in the Gregorian Calendar), and does it much faster than all the other algorithms proposed. Also, each element takes (in principle) only three bits; the entire table thus takes only 1200 bits; on many computers this will be less than the instructions to do all the complicated calculations proposed for the other algorithms.

Incidental note: Because 7 does not divide 400, January 1 occurs more frequently on some days than others! Trick your friends! In a cycle of 400 years, January 1 and March 1 occur on the following days with the following frequencies:

 

           Sun      Mon     Tue     Wed     Thu     Fri     Sat
    Jan 1: 58       56      58      57      57      58      56
    Mar 1: 58       56      58      56      58      57      57

The Gregorian calendar was introduced in 1582 in parts of Europe; it was adopted in 1752 in Great Britain and its colonies, and on various dates in other countries. It replaced the Julian Calendar which has a four-year cycle of leap years; after four years January 1 has advanced by five days. Since 5 is relatively prime to 7, a table of 4 * 7 = 28 elements is necessary for the Julian Calendar.

There is still a 3 day over 10,000 years error which the Gregorian calendar does not take into account. At some time such a correction will have to be done but your software will probably not last that long!

Algorithm for Finding Day of Week

Here is a standard method suitable for mental computation:

  1. Take the last two digits of the year.
  2. Divide by 4, discarding any fraction.
  3. Add the day of the month.
  4. Add the month's key value: JFM AMJ JAS OND 144 025 036 146
  5. Subtract 1 for January or February of a leap year.
  6. Add 4 for 1700's, 2 for 1800's, 0 for 1900's and 6 for 2000's and extend the pattern using 400 as the factor:
    1. if the century is divisible by 400, add 6
    2. if the century - 100 is divisible by 400, add 4
    3. if the century - 200 is divisible by 400, add 2
  7. Add the last two digits of the year.
  8. Divide by 7 and take the remainder.

Now 0 is Saturday, 1 is Sunday, 2 is Monday, and so on until 6.

Define a Public Class DayOfWeek

Using the starter code which defines a public class called DayOfWeek that should have the following public methods:

Start with the starter code which provides some helpful methods:


boolean isLeapYear(); 
// Returns true if the year is a leapYear
public String getDayOfWeek(int day) throws IllegalArgumentException;
// Returns the String that maps to the integer day where 0 is Saturday.

Sample Execution



Enter date ('yyyy-mm-dd'): 2019-01-18
The algorithm says the day is 'Friday'
Try again? (y or Y): y
Enter date ('yyyy-mm-dd'):
2001-04-17
The algorithm says the day is 'Tuesday'
Try again? (y or Y): y
Enter date ('yyyy-mm-dd'):
1976-07-04
The algorithm says the day is 'Sunday'
Try again? (y or Y): y
Enter date ('yyyy-mm-dd'):
1776-07-04
The algorithm says the day is 'Thursday'
Try again? (y or Y): n
Bye!