Day Of Week

<< Dot Demo | LabTrailIndex | One Two Ten Game >>

The Algorithms we are using are explained at Wikipeadia

Method 1: Gauss's Algorithm ( Static class method - "Chapter 4 Style")

Here is Gauss' method:

{$w = (d + \lfloor \frac{13 \cdot m-1}{5} \rfloor + y + \left\lfloor\frac{y}{4}\right\rfloor + \left\lfloor\frac{c}{4}\right\rfloor - 2c) \bmod 7$}

Where
      Y: year-1 for  January or February, 
         year   for  the rest of the year
      d: day (1 to 31)
      m: shifted month (March=1,...February=12), eg. ((month + 9) % 12) + 1     
      y: last 2 digits of Y
      c: first 2 digits of Y
      w: day of week (0=Sunday,..6=Saturday)

The math notation for the floor function is the partial bracket. For example, {$\lfloor 5.9 \rfloor = 5$}. It is very handy that in Java, integer division has a "built-in" floor function.

For those who are just at chapter 4, you haven't seen arrays or "if" statements yet. I have some starter code for the Gaussian method below, so all you have to do is translate the math algorithm into java code:

public class DayOfWeekTester 
{
	public static String dow(int month, int day, int year)
	{
		String[] weekdays={"Sunday", "Monday", "Tuesday",
			"Wednesday", "Thursday", "Friday", "Saturday"};
		int Y=year;
		if (month<3)
			Y--;
		int d=
		int m=
		int y=
		int c=
		int w=
		return weekdays[w];
	}
	public static void main(String[] args) 
	{
		System.out.println("4/24/1982 falls on "+dow(4,24,1982));
		System.out.println("Expected Saturday");
		System.out.println("12/25/1960 falls on "+dow(12,25,1960));		
		System.out.println("Expected Sunday");
	}

}

Method 2: Look up Algorithm ( Object method with arrays)

DayOfWeekTester2.java

public class DayOfWeekTester2 
{

	public static void main(String[] args) 
        {
		Date d1=new Date(4,24,1982);
		System.out.println(d1+" falls on "+d1.dow());
		System.out.println("Expected Saturday");
		Date d2=new Date(12,25,1960);
		System.out.println(d2+" falls on "+d2.dow());
		System.out.println("Expected Sunday");
	}

}

Date.java

public class Date 
{
	private int month;
	private int day;
	private int year;
	public static String[] weekdays={"Sunday", "Monday", "Tuesday",
		"Wednesday", "Thursday", "Friday", "Saturday"};
	public static int[] centuryTable={4,2,0,6,4,2,0,6,4,2};
    public static int[] monthTable={0,0,3,3,6,1,4,6,2,5,0,3,5};

    public Date(int m, int d, int y)
    {
    	    month=m;
    	    day=d;
    	    year=y;
    }

    public String toString(){
		return month+"/"+day+"/"+year;
	}
	public int getCenturyTable()
	{
		int c=year/100;
		return centuryTable[c-17];
	}
	public int getLast2DigitsOfYear()
	{
		return year%100;
	}
	public int leapYearCount()
	{
		return (getLast2DigitsOfYear()/4); 
	}
	public boolean isLeapYear()
	{
		if (year%400==0)
			return false; //Gregorean adjustment
		return year%4==0;
	}
	public int getMonthTable()
	{
		if (isLeapYear() && month==1)
			return 6;
		if (isLeapYear() && month==2)
			return 2;
		return monthTable[month];
	}
	public String dow()
	{
		int sum=getCenturyTable()+getLast2DigitsOfYear()+leapYearCount()+getMonthTable()+day;
		return weekdays[sum%7];
	}
}