For your fourth lab, you will play around with exceptions.
First, create the following Exception classes:
- StringTooLongException subclass of Exception
- Constructor that takes a single parameter "message"
(calls the superclass constructor passing this message)
- EmptyStringException subclass of Exception
- Constructor that takes a single parameter "message"
(calls the superclass constructor passing this message)
Next, create a class MyScanner, with the following methods:
- Constructor, which takes as an input paramter a variable of type
Scanner and an integer maxStringLength
- boolean hasNext(), which returns true if the contained scanner
has a next token
- string nextString(), which calls nextLine() on the contained
Scanner. If the next line has a length of 0, throw an
EmptyStringException. If the next line has a length longer than
the maxStringLength, throw a StringTooLongException. Otherwise,
return the next line
Finally, create a main program that:
- Tries to create a new scanner based on the filename passed in as
a command line parameter. If no file exists, create a scanner
using System.in. You'll need to use try/catch for this!
- Creates a new MyScanner using this scanner, and a maximum string
length of 10 (this should be a constant!)
- Goes through each line of the input file (using hasNext() and
nextString()), printing out all lines that have a length > 0 and
<= maximum string length. Strings that have a length == 0 or
> maximum string length should be skipped. You'll need to use
try/catch for this!
Grading
You will be graded on several criteria:
Coding standards 10
Program Decomposition 10
Be sure to do proper indentation & commenting. Also, no magic
numbers! Use constants instead.
See the
Coding Standards for more
information
Working
Program correctly reads strings from existing file and prints them
out: 20
Program correctly uses System.in if file given in command line
parameter does not exist, or if command line parameter exists and file
does not 20
Program correctly throws on empy strings & strings too long 20
Program correctly catches exceptions thrown by hasNext, does the
correct thing 20
Submission
All file(s) required for
your project should be in the folder
https://www.cs.usfca.edu/svn/<username>/cs112/Lab4/