Lab 21: Method Overloading and Exceptions


Due Tuesday, November 28 at 1: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: Complete the Lab 21 Handout

Write out the output of the different programs in the Lab 21 Handout. You can either turn this in at the beginning of class in the next session, or scan it / take a high-quality picture and submit it through Canvas.

Step 2: Download All Required Code

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

Step 3: Edit InvalidNumberException.java

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

Step 4: Edit NumberValidator.java

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

Step 5: Edit PositiveNumber.java

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

Step 6: Edit NonNegativeNumber.java

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

Step 7: 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 8: 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:

If you want to perform a digital submission of your handout, you can scan it / take a high-quality picture, and submit it as a series of .jpg 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.