CPSC 124, Fall 2001
Programming Assignment 1
This is an individual programming assignment. You can get help from me and from the TA on this assignment. Otherwise, you should work on the assignment on your own. You are not permitted to look at other students' programs or to get help on the assignment from them or to help other students. Doing so will be considered to be committing academic dishonesty (plagiarism).
The assignment is due on Friday, October 5. If you turn it in up to one week after than date, a late penalty of 20% will be deducted from the grade. Assignments will not be accepted after October 12. If you have done some work but do not have a working program, you should turn in what you have for partial credit.
Your program should follow the rules of good programming style, as discussed on the lab worksheets and in class.
Your assignment is to write a program that can print out a little one-month calendar for any given month and year. The program will ask the user to specify a month by inputting a number between 1 and 12. Then the program can get a year, which can be any integer. After getting this input, it will print the calendar. You can run my version of the program by changing to the directory /home/cs124 (using the command cd /home/cs124) and executing the command java Cal. The output of your program must be identical (or as close to it as you can make it) to the output from my program. Here, for example, is the output from my program for month 2 in year 1800:February, 1800 Su Mo Tu We Th Fr Sa 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
To be more precise, you should output the month name and the year on the first line. (Note that you must print the month name, not the month number.) The next line has two-letter headers for the days of the week, with single spaces between the days. Then the calendar itself is printed, using three spaces for each number.
One of the pieces of information that your program needs is the day of the week on which the first day of the month falls. This tells you which column to print the "1" in. I don't know how to compute this, easily, myself, so I can't ask you to do it! So, we will use one of Java's built in classes to do this. Put the following line at the beginning of your program, before the "public class Cal" line:import java.util.GregorianCalendar;
This gives you access to the built-in class GregorianCalendar. After reading the month and year from the user, you can use the following three lines to compute the first day of the week for the specified month and year:GregorianCalendar c; c = new GregorianCalendar(year,month-1,1); firstWeekDay = c.get(GregorianCalendar.DAY_OF_WEEK);
The value of the variable firstWeekDay will then be an integer in the range 1 to 7. A value of 1 means that the first day of the month is a Sunday. A 2 means the first day is a Monday, and so on. (This code assumes that you are using variables named "year" and "month" to store the year and the month specified by the user.)
You will also need to know the last date of the month, which could be 28, 29, 30, or 31. This you can compute. If the month is April, June, September, or November, the last day of the month is 30. Every other month except February has 31 days. For February, the number of days depends on whether or not the year is a leap year. The rules for leap years are a little more complicated than you might realize: If the year is evenly divisible by 400, it's a leap year (and February has 29 days). Otherwise, if the year is evenly divisible by 100, it's not a leap year (and February has 28 days). Otherwise, if the year is divisible by 4, then it's a leap year. Otherwise, it's not a leap year. You should code the algorithm described in this paragraph to compute the last date in the month and year that you are interested in.
The other point of interest is how to print out all the dates from 1 to the last day of the month. Some of this I will let you think about for yourself, but here are a few hints: You have to leave some spaces before printing the "1". The firstWeekDay can be used to determine how many blank spaces to leave. You need to output a carriage return after every seventh item. The easiest way to do this is to keep track of how many items you have printed on a line. If this number is 7, then you need to do a System.out.println() and reset the counter to 0 so that you can start counting items on the next line.
Start working on this project early. It is not usually possible to write a program on the night before it is due. Expect to run into difficulties and plan to leave time to come in and get help.
Linux has a little command-line program called cal that does pretty much the same thing as the program I am asking you to write. If you type in the command cal, it will print a calendar for the current month. If you say cal 2 1800, you will get a calendar for February, 1800. This program can also print a calendar for a full year -- something I am not asking you to do. Use cal 2001 to get a calendar for the entire year 2001.
David Eck, 21 September 2001