So far this semester, we have learned quite a lot about computer programming and Java. We have learned basic Java program structure, primitive data types, expressions, objects, class libraries, control statements (conditional and iteration statements), instance variables, scope, parameters, returns, object references, interfaces, arrays, vectors, and inheritance. We have also learned how to create Java source code (*.java) files, which incorporate these concepts using a basic Integrated Development Environment (IDE) like JCreator LE .
This has been a lot of difficult material. You should be proud of yourself for making it this far. But, unfortunately, our ability to perform input and output (IO) in Java has been limited to input via the keyboard class and output via simple System.out.println() commands.
In this lab, we will take our knowledge regarding IO in Java to the next level. We will first learn how to write a program which obtains user input from the keyboard without using the keyboard class. This means we will learn how to manipulate Input Streams. Then we will learn how to write a program which sends program output somewhere else (in addition to the display screen), such as a file on a diskette or hard drive. This means we will learn how to manipulate Output Streams.
The purpose of this part of the lab is to introduce you to writing programs in Java dealing with Exceptions. This includes Input and Output Streams. To start with, you will write a program that throws an Exception (Doesn't that sound fun!)
public class Lab14
{
public static void main(String[] args)
{
}
public static void exceptional(int num) throws GettingUpThereException
{
}
}
Now, let's discuss Exceptions a little bit. An Exception is an Object, therefore they can be created at any time. There are different types of exceptions each defined by a different class. You can design your own type of exception, and, as a matter of fact, we will be doing exactly that. After the code you have already typed (i.e., after the definition of the Lab14 class), enter the following:
class GettingUpThereException extends Exception
{
GettingUpThereException(String message)
{
super(message);
}
}
What we have done is create a new type of exception that is derived from the parent (i.e., super) class Exception. The definition of the new derived class, GettingUpThereException, is placed within the Lab14 file, but not within the Lab14 class. Now, we will complete the exceptional() method.
The final portion of the first part of this lab is to call the exceptional() method from main().
try {
exceptional(age);
}
catch (GettingUpThereException tom) {
System.out.println(tom.getMessage());
}
Notice that the catch block prints the exception's message
using its getMessage() method.
When you have completed the program, test it with numbers below and above your "old-age" number. Have Fun!
When you finish, show your program to your lab instructor.
In the second part of this lab, you will learn how to read from the keyboard without the Keyboard class. Sounds fun, huh. Don't worry, it's not all that bad. We proceed by modifying Lab14.java as follows:
import java.io.*;
to the top of the file.
InputStreamReader tom = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(tom);
age = in.readLine();
exceptional(Integer.parseInt(age));
C:\files\java\junk\Lab14.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown
age = stdin.readLine();
You need to solve this one on your own, but here's a hint. Java input
throws an IOException exception, therefore the function calling it
must handle that exception. You can look at the
exceptional() method as an example of handling such a
situation. The exceptional() method throws a
GettingUpThereException exception, and
IOException is a different type of exception. But,
remember, all exceptions are derived from the Exception
class, therefore, any exception can be caught by a catch
block with a parameter of type Exception.
When you get your code running, show it to your lab instructor for credit.
As our final modification to Lab14.java, we will write output to a file in addition to the monitor.
PrintWriter out = new PrintWriter(new FileOutputStream("out.txt"));
The statement above can throw a FileNotFoundException exception
(actually, the FileOutputStream constructor may throw that exception).
We must either catch that exception using a try/catch block as we have
done in Parts 1 and 2, or we can continue to throw that exception. In
this case, we will continue to throw the exception rather than catch
it. This means that, should the exception occur, our program will be
aborted. To continue to throw this exception out of main()
we must modify the header of main() to read as follows:
public static void main(String[] args) throws FileNotFoundException
out.println("You entered the number " + age);
out.close();
Run your program, and check the contents of "out.txt" using Windows Explorer. The file will exist in the Lab14 directory but it will not appear in the JCreator FileView.
Open "out.txt" (your output file) by double-clicking on its icon in Windows Explorer. Show the contents of that file to your lab instructor.