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
3
John-Lasseter:~ jlasseter\$ java Weekday 1874 3 7
6
```

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:

• yZ = y - ((14 - m) / 12)

(If the month is January or February, we'll consider them as the 13th or 14th months of the previous year)

• mZ = m + (12 * ((14 - m) / 12))

(This keeps the values for March through December at 3 .. 12. January becomes 13 and February becomes 14)

• B = yZ % 100

(Years since the century began: i.e., the last two digits of y)

• C = yZ / 100

(The century: i.e., the first two digits of y)

• leapDays = (B / 4) + (C / 4) + 5*C

(The total number of leap days that have occurred since 0 C.E.)

• w = (d + ( ((mZ+1) * 26) / 10) + B + leapDays + 6) % 7

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: http://en.wikipedia.org/ 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