CIS-3720 Homework #2 (Input Validation)

The purpose of this assignment is to give you some practice writing, by hand, an input validation method for a non-trivial data type. You should think about using regular expressions to enforce appropriate formatting rules on the input, but also don't forget to check high level constraints as appropriate. See below for more details.

Consider a class Date (define it in package edu.vtc.cis3720 to avoid any possible conflict with another class Date that might happen to be on your CLASSPATH. Instances of the class should represent a date value from January 1, 1900 to Dember 31, 2099. Following the GPS example, define that class as an immutable class (no modifications to an object are allowed after it is constructed) with a suitable static input method. Be sure your definition contains the following features:

  1. A validation method to checks to ensure a given year, month, day are correct and in range. Be sure to handle the fact that the number of days in each month differs, and that February has either 28 or 29 days depending on if it occurs in a leap year.

  2. A constructor taking year, month, and day as integers. It should call the validation method above and throw an appropiately defined exception if the constructor parameters do not specify a valid date.

  3. Accessor methods that allow clients to read the various components of a date ("getters"). Do not define any setters (the class is immutable).

  4. An input method in the manner of the GPS example that reads dates from the given input stream. It should support dates entered in three formats:

    HINT: Define three different regular expressions and then try matching the input against them in order. An error occurs only if none of the regular expressions match.

Submit the Java source file you created.


Last Revised: 2019-09-09
© Copyright 2019 by Peter C. Chapin <pchapin@vtc.edu>