Background Information Zeller\'s congruence (developed by German mathematician C
ID: 3539783 • Letter: B
Question
Background Information
Zeller's congruence (developed by German mathematician Christian Zeller in the late 1800's) gives a way to determine the day of the week of any calendar date. The formula states:
h = (q + [13(m+1)/5] + K + [K/4] + [J/4] + 5J) mod 7
where
h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, etc.)
q is the day of the month
m is the month (3 = March, 4 = April, %u2026 , 12 = December, 13 = January, 14 = February)
K is the year of the century (e.g., K = 13 for 2013)
J is the century (e.g., J = 20 for 2013)
Note that the brackets in this expression mean "floor" - i.e., round down the result of the division to the next integer. The only weird part about this formula is that January and February are treated as the 13th and 14th months of the previous year.
Examples:
July 20, 1969 q = 20 m = 7 K = 69 J = 19 Using these values, Zeller's congruence yields h = 1, which corresponds to Sunday.
February 9, 2000 q = 9 m = 14 K = 99 (since February is treated as the 14th month of the previous year, 1999) J = 19 (since February is treated as the 14th month of the previous year, 1999) Using these values, Zeller's congruence yields h = 4, which corresponds to Wednesday.
The Assignment
Write a Java program that allows the user to enter a year. Your program should then use Zeller's congruence to display a complete calendar for every month of that year. Be sure to account for leap years. Include error checking to ensure that the user cannot enter a year before 1500. Allow the user to repeat the program as many times as s/he wishes.
Please don't try to "brute-force" this program by writing separate code for every single month! Loops are your friends :)
Example program run: Enter the year: 2013
2013 Calendar
January
-------
S M T W R F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
February
--------
S M T W R F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28
March
-----
S M T W R F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
April
-----
S M T W R F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
May
---
S M T W R F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
June
----
S M T W R F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
July
----
S M T W R F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
August
------
S M T W R F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
September
---------
S M T W R F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
October
-------
S M T W R F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
November
--------
S M T W R F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
December
--------
S M T W R F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Enter another year? (1 = yes, any other integer = no) 2
kthxbye!
Coding Style: Remember to use good coding style. This means:
Use descriptive variable names.
Follow standard Java programming conventions for variableNames, ClassNames, CONSTANT_NAMES.
Consistently indent your code. (Pro tip: You can use Ctrl+Shift+I in BlueJ to automatically indent!)
Use comments judiciously.
Explanation / Answer
== Formula ==
For the Gregorian calendar, Zeller's congruence is
:<math>h = left(q + leftlfloorrac{13(m+1)}{5} ight floor + K + leftlfloorrac{K}{4} ight floor + leftlfloorrac{J}{4} ight floor - 2J ight) mod 7,</math>
for the Julian calendar it is
:<math>h = left(q + leftlfloorrac{13(m+1)}{5} ight floor + K + leftlfloorrac{K}{4} ight floor + 5 - J ight) mod 7,</math>
where
* ''h'' is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ...)
* ''q'' is the day of the month
* ''m'' is the month (3 = March, 4 = April, 5 = May, ..., 14 = February)
* ''K'' the year of the century (<math>year mod 100</math>).
* ''J'' is the century (actually <math>lfloor year/100 floor</math>) (For example, in 1995 the century would be 19, even though it was the 20th century.)
NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is February 2, 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format)
For an [[ISO week date]] Day-of-Week ''d'' (1 = Monday to 7 = Sunday), use
:<math>d = ((h + 5) mod 7) + 1</math>
=== Implementation in software ===
The formulas rely on the mathematician's definition of [[Modulo operation|modulo]] division, which means that %u22122 mod 7 is equal to positive 5. Unfortunately, the way most computer languages implement the remainder function, %u22122 mod 7 returns a result of -2. So, to implement Zeller's congruence on a computer, the formulas should be altered slightly to ensure a positive numerator. The simplest way to do this is to replace %u2212 2''J'' by + 5''J'' and %u2212 ''J'' by + 6''J''. So the formulas become:
:<math>h = left(q + leftlfloorrac{13(m+1)}{5} ight floor + K + leftlfloorrac{K}{4} ight floor + leftlfloorrac{J}{4} ight floor + 5J ight) mod 7,</math>
for the Gregorian calendar, and
:<math>h = left(q + leftlfloorrac{13(m+1)}{5} ight floor + K + leftlfloorrac{K}{4} ight floor + 5 + 6J ight) mod 7,</math>
for the Julian calendar.
One can readily see that, in a given year, March 1 (if that is a Saturday, then March 2) is a good test date; and that, in a given century, the best test year is that which is a multiple of 100.
Zeller used decimal arithmetic, and found it convenient to use ''J'' and ''K'' in representing the year. But when using a computer, it is simpler to handle the modified year ''Y'', which is Y - 1 during January and February:
:<math>h = left(q + leftlfloorrac{(m+1)26}{10} ight floor + Y + leftlfloorrac{Y}{4} ight floor + 6leftlfloorrac{Y}{100} ight floor + leftlfloorrac{Y}{400} ight floor ight) mod 7,</math>
for the Gregorian calendar, and
:<math>h = left(q + leftlfloorrac{(m+1)26}{10} ight floor + Y + leftlfloorrac{Y}{4} ight floor + 5 ight) mod 7,</math>
for the Julian calendar.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.