sliderule 7:53pm> cal 1800
1800
Jan Feb Mar
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
Apr May Jun
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
Jul Aug Sep
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
Oct Nov Dec
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
Once your
Cal
class is written, the command
> java Cal 1800
For this assignment, you are to use only the basic Java language tools: primitive types, arrays, strings, System.out, and methods that you write. You probably need to create only one class, and all of that class's methods can probably be static. In short, this assignment is about algorithms, not about interacting objects. In particular, you are not allowed to use the following classes:
Check out the
Cal
project from your individual Subversion repository for the course.
Cal
.
java Cal 3002 3
is equivalent to
java Cal 3002
, while
java Cal 5092 6
produces a 5092 calendar with two rows of six months each and
java Cal 2000 1
produces a 2000 calendar with only one month per row. See the examples at the end of this document.
java Cal 1863 6
, your program should print the 1863 calendar in two rows of 6 months per row.
If you have never written a Java program that uses command-line arguments, or never done so in Eclipse, it might be easiest to have someone show you how to do it. This document that also explains it.
main()
method must call a helper method
printCalendarTo()
. Aside from processing command-line arguments, most of the work of your program will be done in
printCalendarTo()
and other helper methods that your create. The provided JUnit tests call
printCalendarTo()
directly.
The specification is intended to match what the UNIX cal program does (for the C=3 case).
All output is sent to the
PrintWriter
object that is a parameter of the
printCalendarTo()
method. When called from the command-line this
PrintWriter
should be constructed to use
System.out
, as follows:
new PrintWriter(new OutputStreamWriter(System.out));
The output must be as follows:
Test your code by running the included JUnit tests. Submit your code by committing it to your Subversion repository.
if
statements and one
switch
statement.
Be sure to get leap years correct. Note that 1800 and 1900 were not leap years, and 2100 will not be a leap year. Wikipedia has (much) more to say about leap years.
Don't use "magic numbers". Name the constants that your program uses. Here are the all of the field declarations from my code. You do not have to do what I did here, but this might help you to think of some possibilities.
private static final int SUNDAY = 0;
private static final int SATURDAY = 6;
private static final int JANUARY = 0;
private static final int DAYS_PER_WEEK = 7;
private static final int WEEKS_PER_MONTH = 6;
private static final int WIDTH_PER_DAY = 3;
private static final int MONTHS_PER_YEAR = 12;
private static final String MONTH_SEPARATOR = " ";
private static final String WEEK_HEADER = " S M Tu W Th F S ";
private static final String[]
MONTH_NAME={"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
private static boolean isLeapYear;
sliderule 8:50am > java Cal 1904 4
1904
Jan Feb Mar Apr
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 1 2 3 4 5 6 1 2 3 4 5 1 2
3 4 5 6 7 8 9 7 8 9 10 11 12 13 6 7 8 9 10 11 12 3 4 5 6 7 8 9
10 11 12 13 14 15 16 14 15 16 17 18 19 20 13 14 15 16 17 18 19 10 11 12 13 14 15 16
17 18 19 20 21 22 23 21 22 23 24 25 26 27 20 21 22 23 24 25 26 17 18 19 20 21 22 23
24 25 26 27 28 29 30 28 29 27 28 29 30 31 24 25 26 27 28 29 30
31
May Jun Jul Aug
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 7 1 2 3 4 1 2 1 2 3 4 5 6
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 31
31
Sep Oct Nov Dec
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 1 1 2 3 4 5 1 2 3
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
sliderule 8:36am > java Cal 1953 6
1953
Jan Feb Mar Apr May Jun
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 1 2 1 2 3 4 5 6
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
Jul Aug Sep Oct Nov Dec
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31 29 30 27 28 29 30 31
30 31
sliderule 8:51am > java Cal 2831 2
2831
Jan Feb
S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28
Mar Apr
S M Tu W Th F S S M Tu W Th F S
1 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 29 30
30 31
May Jun
S M Tu W Th F S S M Tu W Th F S
1 2 3 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30
Jul Aug
S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 29 30
31
Sep Oct
S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 1 2 3 4
7 8 9 10 11 12 13 5 6 7 8 9 10 11
14 15 16 17 18 19 20 12 13 14 15 16 17 18
21 22 23 24 25 26 27 19 20 21 22 23 24 25
28 29 30 26 27 28 29 30 31
Nov Dec
S M Tu W Th F S S M Tu W Th F S
1 1 2 3 4 5 6
2 3 4 5 6 7 8 7 8 9 10 11 12 13
9 10 11 12 13 14 15 14 15 16 17 18 19 20
16 17 18 19 20 21 22 21 22 23 24 25 26 27
23 24 25 26 27 28 29 28 29 30 31
30