CS124, Fall 2010
Assignment #6
DUE: Thursday, November 4.
Write a program to output a calendar for a given month. The user is to specify a month (as a string with the name of the month in a capital letter, the rest in lowercase) and the day of the week on which that month starts (as a number from 1 to 7). A sample run of the program might be,
Please enter the month : May
Please enter the day of the week on which that month begins : 2
Here is the calendar for that month :
May
Sun Mon Tue Wed Thu Fri Sat
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
Note that the program "knows" that there are 31 days in May.
The must be under no obligation to supply this information.
There are many ways such a program could be written. The more well-organized
your program is in terms of objects and classes, the better.
The highest grades will go to those that consist at least of the following
two classes:
- A class Month, that is capable of printing a month
(as above) given a specification of the day of the week on which
the first day of the month falls (e.g., 1 for Sunday, 2 for Monday,
etc.). The Month class must contain at least these
methods:
- A default constructor that initializes the month to "January"
and the initial day to 1.
- Another two-parameter constructor that initializes the month to
the first parameter (given as a String) and the beginning day to
the second parameter (given as an int). Thus the header for
this non-default constructor should look like:
public Month (String theMonth, int initialDay)
And to create an object for printing out a month like the example
above, you would write,
Month myMonth = new Month("May", 2);
- A public method printMonth() that prints out the month in
the format indicated above. Thus, given the above statement,
myMonth.printMonth() would print the sample given in the sample run. Two tools will come in handy here.
- Make use of tools for formatting so that each number has a fixed
number of spaces assigned to it, and so that it is right-justified.
You have at least two choices here: One is the method System.out.printf(). Alternatively, there is the
NumberFormat class.
Both can be used to align the numbers
as shown in the example above.
- Secondly, there's a trick
for getting the dates output via a single for-loop, using a
counter and the mod-operator to put in the line breaks.
These tools will be illustrated in lecture.
- A private support method daysInMonth() which returns (as an integer
value) the number of days in the month. (You know the algorithm for this,
of course: "30 days hath September, April, June and November, etc...." Take
this rhyme and express it as an if-statement. Assume February has 28 days;
don't worry about leap years unless you want to -- see below.) This method will come in handy in writing
the printMonth() method.
- Note that this basic model of a "Month" is very simple. There are only three public methods: two constructors, and printMonth()!
- A driver program named Calendar.java that demonstrates
the Month class, by prompting the user for the name of the month, the
starting day, setting the month and starting day for the calendar object,
and finally invoking printMonth() for
that object. Make it so that the user can do this any number of times.
When you go about coding these classes, remember the lesson of
Lab 3:
- Write one method at a time.
- Do not write a new method until you have already tested the ones you have
already written.
BONUS PROBLEMS : Allow the user to specify if it's a leap year.
Make Calendar capable of writing out the calendar for any user-specified number of months, given a starting month and starting day for that month as above.
(These options, especially that last one,
may require significant changes in the suggested
implementation of the Month class above. Do not attempt this unless you have
already done your best to implement the basic features.)
Submit electronically as usual.
Back to CS 124 Home Page