Your job in Part 1 will be to add specialized exceptions so that when the program crashes on bad input, the stack trace is a little more informative. Your job in Part 2 will be to add real error handling, and some nice features like the ability to read input from files, which will require still more use of exceptions!
The ModQueue class extends Queue in three ways: 1) Duplicate entries
are not allowed. If you try to enqueue a string that's already in
the queue, the enqueue is ignored. 2) A method void
dequeue(String s) has been added, which dequeues everything
up to and including entry String s in the queue.
If s is not in the queue already, this could cause problems!
3) A method String dump() has been added, that
returns a String that is a comma-separated list of the values in
the Queue at that point.
Compile the program and run the program for yourself. Here are some example runs:
~/$ java Lab09a > add foo > add bar > add goo > dump foo,bar,goo > clearto bar > dump goo > quit |
~/$ java Lab09a > add foo > add bar > dump foo,bar > clearto bat Exception in thread "main" java.lang.NullPointerException at Queue.dequeue(Queue.java:16) at ModQueue.dequeue(ModQueue.java:5) at Lab09a.main(Lab09a.java:17) |
~/$ java Lab09a > dump Exception in thread "main" java.lang.NullPointerException at Queue$Iter.next(Queue.java:32) at ModQueue.dump(ModQueue.java:12) at Lab09a.main(Lab09a.java:25) |
~/$ java Lab09a > add foo > add bar > dump foo,bar > clearto bat Exception in thread "main" QueueException: dequeue empty queue at Queue.dequeue(Queue.java:16) at ModQueue.dequeue(ModQueue.java:5) at Lab09a.main(Lab09a.java:20) |
~/$ java Lab09a > dump Exception in thread "main" QueueException: iterator past end of queue at Queue$Iter.next(Queue.java:34) at ModQueue.dump(ModQueue.java:12) at Lab09a.main(Lab09a.java:28) |
Important: You should notice that we are able to make these changes and have everything work, even though ModQueue cannot be changed. In fact, we are doing it without access to the source code, or any way to even just recompile it. That should make crystal clear that we have distance (on the call stack) between where the errors occur (in Queue methods) and where they will ultimately be handled in Part 2 (Lab09's main). The fact that we are able to deal with this shows the strength of this approach to error handling.
File 'foo.txt' could not be opened; switching input to standard in.and the source of the commands should switch over to being standerd in.
Scanner sc = null;
try { sc = new Scanner(new FileReader(fname)); }
catch(IOException e) { e.printStackTrace(); System.exit(1); }
... and adapt it to this problem.
dump command when the queue
is empty, or the clearto with a string
that isn't in the queue, the program shouldn't crash. Instead, it
should silently continue, except that, in verbose mode, issuing a
clearto command with a string that's not in the qeueue should
result in the error message "String 'foo' not found!"
where, of course, foo should be replaced with the actual
clearto string. After printing the error, however the program
should continue running as if nothing bad happened.
Note: in the clearto case, the queue
should be emptied by a clearto command with a
string that's not in the queue.
add and the end-of-file occurred when
trying to read the string to be added,
the program should print the message
"Unexpected end of input.".
unknown command 'foo'.",
where "foo" should be replaced by the given command. In both
cases the program should simply continue to process new commands.
.empty() method
on Queue or ModQueue objects! I'm forcing you to use exceptions.
| Example: stdin | Example: stdin | Example: non-existent file, switch to stdin | Example: ex1.txt | Example: ex2.txt |
~/$ java Lab09a > add cat > add dog > clearto rat > dump > ctrl-d |
~/$ java Lab09a -v > add cat > add dog > clearto rat String 'rat' not found! > dump > ctrl-d |
~/$ java Lab09a -v asdf File 'asdf' could not be opened; switching input to standard in. > add cat > add dog > damp Unknown command 'damp'. > dump cat,dog > quit |
~/$ java Lab09a ex1.txt
foo,bar,bat,cat
bat,cat
|
~/$ java Lab09a -v ex2.txt
foo,bar
String 'cat' not found!
Unexpected end of input.
|
submit -c=IC211 -p=lab09 *.java