CSSE 230
Cal Programming Assignment

Overview

Write a Java application that duplicates and extends some of the functionality of the UNIX cal command. When called with one numeric argument, the command displays a calendar for the indicated year. It should work for any year after 1799. For example:

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
should produce output that is the same as above.

Objectives

Tools

Most assignments in CSSE 120, 220, and 221 involve discovering and using tools that are part of the JDK API libraries. There are, in fact, some higher-level tools that could make writing this program easier. But, consistent with the "under the hood" nature of this course, you will do some of the kinds things that the people who wrote those tools did.

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.

Detailed specifications

Turnin

Test your code by running the included JUnit tests. Submit your code by committing it to your Subversion repository.

Challenge — for 10 Bonus Points, and Stretching Your Brain!

Can you do this simply and efficiently without using any 2D arrays (or their equivalent), and only using a small number of small one-dimensional arrays (no array should need to have a length that is larger than 12)? For example, my code only has one array of month names and one array of C integers (that is, the length of that array is the number of months per row). Except for the part of the code that processes the command line arguments and checks them for valid values, my entire program contains only three if statements and one switch statement.

Hints for Making Your Code Flexible and Readable

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;
  

Grading

Here is the grading rubric, in Word or PDF format, that we will use for the assignment.

More examples

 
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