CMPS 144L Spring 2019
Lab #2: IncDate, SafeDate, and Exception Handling

Links to Java classes needed in this lab:

Activity #1: Finish IncDate

In last week's lab, Activity #3 was to supply the body of the increment() method in the IncDate class, which was taken from the Dale/Joyce/Weems textbook. Some students did it correctly, others incorrectly, and still others did not get to it.

This time, the increment() method is given in full, but it makes a call to the method isLastDayOfMonth() that is lacking a body. You are to supply that body. Due to the existence of leap day, the required logic is a little tricky.

You can test your work using jGrasp's Workbench feature or the IncDateTester application (a link to which is above). Of course, the input values that you provide when running this application should be chosen to maximize the likelihood that any logic errors in your isLastDayOfMonth() method will be exposed.

When you are finished, submit your completed IncDate.java file to the Lab #2 folder.


Activity #2: Improve SafeDate

The textbook authors present a SafeDate class in Chapter 1 for the purpose of illustrating the throwing and handling of exceptions. Specifically, the class's constructor could detect, in some cases, whether the parameters passed to it described an invalid calendar date. When it did detect this, it threw a DateOutOfBoundsException (which was then handled in the client application program shown by the authors).

Your course instructor has modified the SafeDate class to make it a child of IncDate and so that its constructor does a better job of detecting cases when the parameters it receives do not describe a valid calendar date. In particular, after verifying that the month and year values make sense, it checks to see whether the day value makes sense with respect to the month and year.1. As in the authors' original version, if the date described is not valid, the constructor throws a DateOutOfBoundsException with an appropriate associated message.

What is left for you to do is

  1. supply a short code segment (where the comment MISSING CODE appears) that checks to see if the day value is valid and, if not, throws a DateOutOfBoundsException,
  2. provide the body of the isValidDay() method. Some helpful hints are included in the comments preceding the method.

When you are finished, submit your completed SafeDate.java file to the Lab #2 folder.


Activity #3: Improve SafeDateTester

As it stands, the SafeDateTester application prompts the user to enter integer values for month, day, and year in order to describe a calendar date. It then passes those values to the constructor of the SafeDate class in order to create an instance of that class representing that calendar date. If the described calendar date is valid, it is printed in M/D/Y format and the program terminates. Otherwise, it repeats the process just described. Thus, the program will not terminate until such time as the user has entered inputs describing a valid calendar date.

Oh, except that the program will terminate if the user causes it to crash by entering an input string that cannot be interpreted as a sequence of three integer values. Specifically, the nextInt() method of the Scanner class will react to non-integer input by throwing an InputMismatchException (from the java.util package), which will make the program abort.

Your task is to modify the SafeDateTester program so that, rather than aborting in that situation, it prints a message telling the user that (s)he entered non-integer input and repeats the behavior of asking the user for input, etc.

To do this, you will need to insert a new catch block immediately after the one that catches DateOutOfBoundsException. You will need to make a few other adjustments to the code, too.

When you are finished, submit your completed SafeDateTester.java file to the Lab #2 folder.


Activity #4: Complete 2nd Constructor in Date Class

In the Date class you will notice that there is a second constructor (that is not in the textbook authors' version). The intent is for it to accept (via its formal parameter) a single number and to initialize the newly created Date object so that it represents the calendar date having that as its Lilian Day Number. What has been given to you does not do that, however. You are to attempt to correct that.

The problem you face here is to translate a Lilian Day Number into values representing month, day, and year. Once you have those values, you can assign them to the corresponding instance variables.

What is suggested is that you make repeated calls to the lilian() method (the one having three parameters) as a way of searching for the correct (month,day,year) combination. For example, suppose that you were to make a first "guess" by calling lilian(2, 8, 2019) and that the resulting value was less than the Lilian Day Number that you were searching for. Then your next guess should be a date that comes after Feb. 8, 2019. And you can make an intelligent guess by taking account of the difference between the value returned by lilian() and the number that you're looking for. Repeat this process to "home in" on the right date.


Footnote

[1] Whether or not a month value is valid is independent of the year or day, and similarly the validity of a year value does not depend upon the month or day. But to determine the validity of a day value, one must take note of the month and, sometimes, the year.