This is not a current assignment. If you are currently enrolled in SE1020, do not do this assignment expecting to receive credit for it.

Lab 4: Date Calculator Exception Handling

Objectives

Learn how to incorporate exception handling into a Java program.

Assignment

Download the source files for the Date Calculator application whose user interface is shown below. In Eclipse, create a project containing these files, build the application, and execute it. The goal of the application is to compute the number of days between two dates. Test the program by entering various values for Start Date and End Date. Note how fragile the application is - that is, you can easily make it crash by entering values that don't conform to the expected format. Besides crashing, you can make it produce negative values by entering an End Date that precedes the Start Date, and you can specify dates that are invalid, such as “13/32/1999” (month and day out of range). For our purposes, all these behaviors should be considered incorrect.

Basic UI

Your goal for this lab is to incorporate error checking, exception handling, error reporting, and error recovery into the application so that it responds “correctly” to badly-specified input. Specifically, the application must

  • detect and report errors in Start Date and End Date input syntax. The accepted syntax is MM/DD/YYYY (e.g. “07/04/1776”), and invalid entries (e.g. “07041776”, “123”, “July 4 1776”, or “abcd”) must be detected. Upon detection, an error message must be presented to the user that specifies whether the error was present in the Start Date or End Date. If an error exists in both Dates, it is acceptable to report only the error in the Start Date.
  • detect and report errors in values specified for MM, DD, and YYYY. Upon detection, an error message must be presented to the user that specifies whether the error was present in the Month, Day, or Year specification. If errors exist in more than one value, it is acceptable to report only the first error detected. Simple error checking is all that is necessary; that is,
    • values for YYYY must be greater than 0
    • values for MM must lie between 1 and 12
    • values for DD must lie between than 1 and 31
  • detect and report errors in Start/End Date specification for the case where the specified Start Date is chronologically later than the End Date. Upon detection, and error message must be presented to the user specifying that the End Date preceded the Start Date.

The UML Class diagram for this application is shown below. Note that this application is structured in a pattern that separates responsibility for various functionality among three classes. That is, bullet the DateCalculatorApp class is responsible only for creating the UI and presenting information to the user (the other two classes do not present any information to the user)

  • the EventHandler nested class is responsible only for handling user input (any errors are delegated back to the DateCalculatorApp)
  • the DateToDayConverter class is responsible only for handling the computation of the number of days between dates (any errors are delegated back to the EventHandler class, which then delegates back to the DateCalculatorApp)

class diagram

The UML Sequence diagram for the “Normal Flow” for the Calculate event (i.e. the case where no input specification errors exist) is shown below. This also illustrates how the program you downloaded works, provided no errors are encountered.

Sequence diagram - Normal Flow

The UML Sequence diagrams for the “Alternate Flows” for the Calculate event are shown below. Each of these represents a different case of how input errors occur, and how they should be handled. The program you downloaded does not behave this way yet, since it doesn't implement any error handling. It's your job in this lab to fix that.

Sequence diagram - Alternate Flow

Sequence diagram - Alternate Flow

Sequence diagram - Alternate Flow

Your assignment is as follows: In the Java files you downloaded, no error handling has been implemented. Instead, find the comments containing the word “TODO” that indicate where you need to insert code that will incorporate proper error handling. Follow the detailed instructions contained within the comments; they tell you how you should Incorporate error detection, exception handling, and exception generation into the code to trap all possible types of errors in input and correctly report them so that the program will never crash or produce erroneous results.

Lab report

You should follow your instructor's instructions for your lab report.

Dr. Suri Dr. TaylorDr. Hornick

If you have any questions, consult your instructor.

se1020labs/lab4.txt · Last modified: 2009/06/03 11:22 (external edit)
 

This website is not owned or managed by the Milwaukee School of Engineering.

© 2003-2010 Dr. Christopher C. Taylor, et. al. • Office: L-343 • Phone: 277-7339 • npǝ˙ǝosɯ@ɹolʎɐʇ • -> RSS <-