next up previous
Next: Using objects as parameters Up: Assignment 9: Objects Previous: Assignment 9: Objects

System objects

System is the name of the built-in class that contains methods and objects used to get input from the keyboard, print text on the screen, and do file input/output (I/0).

System.out is the name of the object we have used to print text. When you invoke print and println, you invoke them on the object named System.out.

Interestingly, you can print System.out:

System.out.println (System.out);
The output is:

java.io.PrintStream@80cc0e5
As usual, when Java prints an object, it prints the type of the object, which is PrintStream, the package in which that type is defined, java.io, and a unique identifier for the object. On my machine the identifier is 80cc0e5, but if you run the same code, you will probably get something different.

There is also an object named System.in that has type BufferedInputStream. System.in makes it possible to get input from the keyboard. Unfortunately, it does not make it easy to get input from the keyboard. First, you have to use System.in to create a new InputStreamBuffer.

    InputStreamReader in = new InputStreamReader (System.in);
Then you use in to create a new BufferedReader:

    BufferedReader keyboard = new BufferedReader (in);
The point of all this manipulation is that there is a method you can invoke on a BufferedReader, called readLine, that gets input from the keyboard and converts it into a String. For example:

    String s = keyboard.readLine ();
    System.out.println (s);
reads a line from the keyboard and prints the result.

There is only one problem. There are things that can go wrong when you invoke readLine, and they might cause an IOException. There is a rule in Java that if a method might cause an exception, it should say so. The syntax looks like this:


\begin{verbatim}public static void main (String[] args) throws IOException {
\end{verbatim}
This indicates that main might ``throw'' an IOException. You can think of throwing an exception as similar to throwing a tantrum.

1.
Start with a nice clean copy of the palindrome program. If you didn't get it working, use my solutions to patch it up (they are in notes22.txt). Add a line at the beginning of the file that says import java.io.*;. That means we should import all the classes in the java.io package.

2.
Type in the code on the previous page and play around with it. What error message do you get if you forget to declare that main throws an exception? What if you forget the import statement? What happens if you type weird keys into readLine? Is there anything you can type to cause an IOException?

3.
Write code that generates the following output:

Type a word or phrase.
I will check and see if it is a palindrome.

> banana

You typed: banana
banana is not a palindrome.

4.
Put your code in a loop so that it prompts the user to type a word over and over, and it checks each word for palindromity.

5.
Add a condition to the loop so that if the user types the word ``stop,'' the program terminates. Remember that you cannot use the == operator to compare Strings.


next up previous
Next: Using objects as parameters Up: Assignment 9: Objects Previous: Assignment 9: Objects
Allen B. Downey
1999-04-07