Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Bonus Lab #1
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

There's no due date on this lab, and there's nothing to turn in, though I'm happy to discuss your work on it at any time. It's presented here as an extra study exercise.

Part One

Write a program, Weekday, that will tell you the day of the week on which a date occurred. Specifically, your program whould take three integer arguments representing a date. The arguments should be given in the order year month day. The program should print a single integer: 0 for Sunday, 1 for Monday, and so on, up to 6 for Saturday.

Example Interactions

John-Lasseter:~ jlasseter$ java Weekday 2014 9 17
John-Lasseter:~ jlasseter$ java Weekday 1874 3 7

Techniques and Advice

Let's assume we already have the values m (month), d (day) and y (year). Then the weekday for m/d/y (which we'll denote by w) can be found by calculating the following values:

All of the divisions above are integer divisions (i.e. with the fractional parts discarded). Hence any variables you use should be of integral type (if you use double values, the formulae won't be quite right).

This formula, known as "Zeller's Congruence", is due to Julius Christian Johannes Zeller, a German mathematician of the 19th century. Although opaque, it is possible, with a little work, to understand how he found it. For the curious, the Wikipedia entry is quite nice: wiki/Zeller_congruence

Let's start with the data types you'll need. You're working with remainders and integer division, so the best choice is clearly int. So we'll start out the program as

public class Weekday {
  public static void main(String[] args) {
    int y = Integer.parseInt(args[0]);
    int m = Integer.parseInt(args[1]);
    int d = Integer.parseInt(args[2]);

Finish the rest.

Part Two

Chances are that you modeled every value in your program using the int data type (if not, you should have). What happens if instead you declare every value as a double? What do you think is going wrong? Specifically, which lines of your program have errors in their calculations?

John H. E. Lasseter