Login to class.mimir.io. Open Lab1: DayOfWeek project. Instructions are given below for reference.
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!
Here is a standard method suitable for mental computation:
Now 0 is Saturday, 1 is Sunday, 2 is Monday, and so on until 6.
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.
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!