Lab 21: Exceptions


Due Friday, May 4 at 11:59 PM

Goals for This Lab

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Download All Required Code

Download all the provided .java files under the Provided files listing above.

Step 2: Edit InvalidNumberException.java

You will need to define a custom class. The comments in InvalidNumberException.java provide more details.

Step 3: Edit NumberValidator.java

You will need to define a custom abstract class. The comments in NumberValidator.java provide more details.

Step 4: Edit PositiveNumber.java

You will need to define a custom class. The comments in PositiveNumber.java provide more details.

Step 5: Edit NonNegativeNumber.java

You will need to define a custom class. The comments in NonNegativeNumber.java provide more details.

Step 6: Run NumberValidatorMain.java

Run the main method in NumberValidatorMain.java. This code takes two command-line arguments:

  1. Something it will try to parse as a positive number
  2. Something it will try to parse as a non-negative number

If a number cannot be parsed, it will output an appropriate error message, depending on what was wrong. For example, underneath the command-line arguments 2 3 this will produce the following output:

Parsed and valid: 2
Parsed and valid: 3

As another example, the following is the output for the command-line arguments foo -1:

java.lang.NumberFormatException: For input string: "foo"
Is negative: -1

As a third example, the following is the output for the command-line arguments 0 0:

Not positive: 0
Parsed and valid: 0

Step 7: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 21”. From here, you can upload your code. Specifically, you must turn in the following four files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.