CS 115

Homework 4

Due Wednesday, March 7, 2007 no later than Noon through WebCT

PURPOSE

This assignment has several purposes:

1.  To reinforce the ideas and language concepts introduced in the previous assignment

2.  To introduce the use of a single GUI method.

3.  To introduce the ideas and language concepts for exception handling and parsing.

ASSIGNMENT

Write two more versions of Zeller’s Congruence.

THIRD VERSION

Write a class named DayOfWeek3 that improves upon the programs written in the previous assignment. In particular, this version will have the user enter a date in the American version of the conventional slash-separated month/day/year format. Your program must parse this input in order to determine the values of the integer variables that are used to compute the congruence. Use the various methods of the String class, described in Sections 5.1 and 5.2, to do the parsing.

The basis of your parsing algorithm should be the observation that a properly formed date must have exactly two slashes that separate the input into three substrings. Parsing should proceed something like this:

1.  Use indexOf to find the first slash. If there is no slash then the input is illegal; print a message saying so and re-prompt.

2.  Use substring to get the first substring, which should be the month. Use trim to clean up any extra whitespace in the substring.

3.  Use indexOf to find the second slash, starting your search one character past the first slash. If there is no second slash then the input is illegal; print a message saying so and re-prompt.

4.  Use substring to get the second substring, which should be the day, Use trim to clean up any extra whitespace in the substring.

5.  Use indexOf to search for a third slash, starting your search one character past the second slash. There should be no further slash. If there is a third slash then the input is illegal; print a message saying so and re-prompt.

6.  Use substring to get the third substring, which should be the year. Use trim to clean up any extra whitespace in the substring.

Parsing should do nothing more than separate the input into three substrings. Analysis of the substrings to determine if they are numeric and specify sensible dates should be done afterward.

The second major improvement in this version is that your program should be impervious to any bad user input. The programs in the previous assignment would crash with an uncaught exception if the user entered letters when prompted to enter a number. This version of the program should continue to prompt the user until s/he enters a syntactically and semantically good input. You will have to write code to catch NumberFormatException that may be thrown by Integer.parseInt(). Also, your program should tolerate any excess whitespace included in the input. Excess whitespace should be removed when the date is repeated in the output.

A final improvement is that the user should be able to abbreviate years in the current century by writing, for example, “07” instead of “2007.” So 2007 can be entered as 2007 or 07.

Use DayOfWeek2.java from the previous assignment as your starting point. If you last assignment did not work, see me or the TA to fix those errors.

FOURTH VERSION

Write class DayOfWeek4 that improves DayOfWeek3 in two ways. First, it employs a graphical user interface. Second, it can handle all dates from 1/1/1 to 12/31/9999

GUI Methods

Java’s GUI library is called Swing. GUI concepts, and hence Swing, are complex. For now, we will make blind use of some simple Swing methods without trying to understand the big picture. To prompt for user input, you must place this line

import javax.swing.JOptionPane;

At the top of your source file, then make a call similar to this one both to prompt the user and gather the user’s typed input:

String input;

input= JOptionPane.showInputDialog(

null,

“Enter date (m/d/y):”,

“Input”,

JOptionPane.QUESTION_MESSAGE);


This call will popup a dialog box like this one:

Notice that the second argument appears as the prompt message. The third argument is the box’s title, appearing in the title bar (not shown here).

To display the day corresponding to the date, use something like this:

JOptionPane.showMessageDialog(

null,

day,

“Output”,

JOptionPane.INFORMATION_MESSAGE);

where day is a String that contains the output message. The result will look like this:

Extending Into Julian Years

For every date in the range 1/1/1 to 12/31/9999, your answer should be identical to that displayed by a calendar program such as that at www.timeanddate.com/calendar. England and its colonies followed the Julian calendar until September 2, 1752. Once a Julian date has been converted to its Gregorian equivalent, you need only feed the altered date into Zeller’s Congruence to get the correct day of the week.

If the user enters a date in the range September 3-13, 1752, your program should indicate that the date is illegal, just as is February 29 in non-leap years.

Note that, unlike previous problems, you are not being given the algorithm to use. The hard part of this problem is figuring out how to convert Julian dates to Gregorian. Once you have figured out how to make the alteration, expressing the algorithm in Java is not too hard.

The Julian calendar makes every multiple-of-four year a leap year. Recall that the Gregorian rule for leap years does not add a leap day in years that are multiples of 100 but not also multiples of 400. This means that 1700, for example, was a leap year but 1800 was not. The difference in leap years between the two calendars introduces significant complication: it means that Zeller’s Congruence will be ever more inaccurate the further you go back in history through years that are Julian leap years but are not Gregorian leap years – adding a fixed amount to the Julian date before giving it to the congruence will work for dates in 1752 back to February 1700 but on February 18, 1700 the congruence will be off by 1. On February 19 1500 the congruence will become off by w. and so on. I suggest that you experiment with various dates in February of years that are Julian leap years but not Gregorian leap years: run Zeller’s Congruence on the date and compare the result to what the calendar program says. You should eventually be able to identify each date in the range 1/1/1 – 9/21/1752 where the two calendars differ by an extra day.

ADVICE

Three general pieces of advice about programming in general:

1.  Start early.

2.  Write the logic of the program in pseudo-code and then proceed to the next step of writing Java code.

3.  Do NOT write the whole program and test it only then. Write a few lines that can be tested by themselves, then test, then write a few more lines, etc. Program development will proceed steadily and systematically if you do it this way: you will always have a clear idea what part of your code is tested and working and what part is new and possibly full of bugs. If you have errors, look at the new code first.

4.  Do not put all of this code in main. Write several smaller methods that can be tested incrementally in main. Place your main program and the additional methods in a folder named Assignment 4 in your assignments or homework folder. This way everything is together for this assignment.

5.  Think about using some static variables to communicate between your methods.

GRADING

Some people may not correctly complete both parts of the assignment. Do not panic if you do not get the fourth version working completely. (DO panic if you do not get the third version working.) For the fourth version, if you do not get it working completely, submit a text file that indicates what parts of the problem you think you have solved correctly. It is better to hand in a program that is correct in what it does, even if it does only a subset of the overall assignment.

You will be graded in part on programming style:

1.  whether you have chosen good variable names

2.  if the layout/indentation makes the structure clear

3.  if your comments are accurate, not too many and not too few

Submit your source files via WebCT. Do not submit anything else.