CPSC 170 Lab 11: Exceptions and I/O

As usual, create a lab11 subdirectory for today's lab, open this document in Netscape, and start emacs.

In this lab you will explore Java's exception handling features and some of its I/O classes and methods.

  1. File LetterCounts.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. (Note that it converts the word to all upper case first.) Save it to your directory and study it, then compile and run it to see how it works. (In reading the code, remember that to convert from an upper case letter to an integer in range 0..25 you just subtract 'A'.)
    1. Now run it and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArrayIndexOutOfBoundsException, because a non-letter will generate an index that i not between 0 and 25. But it might be handy to count just the letters in an arbitrary phrase. One way to do this is to catch the ArrayIndexOutOfBoundsException and do nothing with it. Modify this method to do this; you will need to:
      • Put the body of the first for loop in a try.
      • Add a catch that catches the exception, but don't do anything with it.
      Compile and run your program.

    2. Now modify the body of the catch so that it prints a useful message (e.g., "Not a letter") followed by the exception. Compile and run the program. Although it's useful to print the exception for debugging, when you're trying to smoothly handle a condition that you don't consider erroneous you often don't want to. In your print statement, replace the exception with the character that created the out of bounds index. Run the program again; much nicer!

    Print LetterCounts.java.

  2. Look at the definition for the Java Stack class on p. 738. Note that the peek and pop methods throw EmptyStackException. This is the clean way to handle trying to pop from an empty stack; instead of returning an arbitrary value, throw an exception that the user can check for if they want, otherwise an error will result. Note two things: When you wrote your stack and queue classes in labs 9 and 10, you had no clean way to handle popping from an empty stack or dequeuing from an empty queue, so we just ignored it. But now you can do better! Retrieve your IntQueue class from lab 10 (either the array or linked version is fine) and modify your dequeue method so that it checks if the queue is empty first, and if so, throws a QueueEmptyException. Note that you will first have to define a QueueEmptyException class, which you should put in its own file. Look on p. 389 of the text (the OutOfRangeException class) for an example of how to define an exception class. The only difference is that your QueueEmptyException class should extend RuntimeException, not Exception. Now modify your IntQueue class so that dequeue checks to see if the queue is empty, and if so, throws a QueueEmptyException. Modify your QueueTest class so that it dequeues from an empty queue, and see what happens. Now make it handle the exception. Note that it will have to import the QueueEmptyException class to do this.

    Print your QueueEmptyException, IntQueue, and QueueTest classes.

  3. Look at the definition of the BufferedReader class on p. 629. The most useful method in BufferedReader is readLine, which returns a String representing a single line of text. Recall that you can construct a BufferedReader from the standard input stream as follows:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    1. Write a simple program that uses a BufferedReader to read in and echo (print back out) lines of text. Stop when the user enters the line "quit". If an IOException occurs, simply print the exception.

    2. Implement a simple readInt method that uses a BufferedReader to read a line of text, then uses a StringTokenizer to extract the first token, convert it to an int, and return it. This is more or less what the Keyboard class does for its readInt, but it keeps the line around for subsequent reads. Test your method in the main program.
    Print this program.

HAND IN: