CS 124, Spring 2013
Lab 5: Files, Exceptions, Applets
There are three exercises on this lab. The first exercise asks you to use a try..catch statement and to use TextIO to read from a file. The use of files with TextIO was covered in the textbook in Subsection 2.4.5, but we skipped over that section when we first covered Chapter 2.
In the second exercise, you'll be drawing more random works of art. For this lab, you will be using for loops, including nested for loops. For the third exercise, you will create a web page that shows your random art program as an applet.
Make a lab5 directory, as usual, to hold your work for this lab. Copy the contents of the directory /classes/cs124/lab5-files into your lab5 folder. You can do that, for example, by giving the following command while working inside your lab5 directory:
cp -r /classes/cs124/lab5-files/* .
This will copy TextIO.java, RandomArt.java, cs124.html and a sample data file named numbers.dat into your lab5 folder.
Your work for this lab will be collected next Saturday, March 2.
Exercise 1: Trying Files
TextIO can be used to read and write files. As a first exercise in file reading, you will read some numbers from a file and compute some basic statistics about the numbers. We did a very similar example in class, except that the numbers in that example were input by the user instead of read from a file. Reading numbers from a file is no different, except that you have to tell TextIO to read its input from the file instead of from the user.
Your program should be able to read from any data file. The file should be specified by the user. The program should start by asking the user to type in the name of the file. The name will be a string and can be read using TextIO.getln(). Then use the command TextIO.readFile(fileName) to tell TextIO to read from the file, where fileName is the name of the file.
After the statement TextIO.readFile(fileName) has been executed successfully, TextIO will be reading from the file. Here is what it should do: Assume that the file contains a list of non-zero real numbers, with a zero at the end to mark the end of the list. Read all the numbers, using TextIO.getlnDouble(), stopping when you get to the zero. You should say how many non-zero numbers there were, and you should report their sum, their average, their maximum value, and their minimum value.
Now, it is possible that the user enters the name of a file that does not exist. In that case, TextIO.readFile(fileName) will throw an exception of type IllegalArgumentException. It is also possible that the file exists but does not contain legal data. In that case, TextIO.getlnDouble() will throw an exception of type IllegalArgumentException.
Ordinarily, an exception will crash the program. However, your program should use try..catch to catch the possible exception. If an exception occurs, print an error message, and exit from the program.
You can give better error messages if you use two try..catch statements -- one for opening the file and one for reading data from the file. In the first case, the error is that the specified file cannot be read. In the second case, the error is that the file does not contain legal data for this program.
You can run your program using the sample data file numbers.dat, which you should have copied into your lab5 folder.
Exercise 2: Random Art
The file RandomArt.java is a program very similar to the random shape program that you worked on in Lab 3. (One difference is that I draw a black border around each shape, to make them stand out better.)
For exercise 2, you should edit the file RandomArt.java. (Do not change the name!) When complete, the program will be able to draw at least three different types of random art. The program should decide at random which type of art to draw. For one type of art, you can use the code that is already there, which draws a lot of random circles and squares. You should write new code for the other two types of art.
For the second type of art, you should draw a large number of lines. For each line, you should select the endpoints of the line at random. Here is an example in which I used a different random color for each line:
For the third type of art, you should fill the window with randomly colored squares, and you should draw a large randomly-colored, randomly-selected letter in each square. Here is an example. In this example, I also drew white grid lines at the end to separate the squares:
For the second type of art, you should use a basic for loop to draw the lines. For the third type of art, you should use a pair of nested for loops. We did a very similar example in class. We also talked about the problem of selecting a letter at random.
You will need to use a large font to draw the letters for the third case. Recall that you can set the font with a setFont command such as:
g.setFont( new Font("Serif", Font.BOLD, 50 );
Exercise 3: Your Art on the Web
The file cs124.html contains a simple Web page that shows RandomArt as an applet. After you have modified and compiled your version of RandomArt.java, you can put your work on the Web so that anyone in the world can see it. Your account contains a folder named www. Anything that you put in that folder is publicly accessible on the Web.
The applet version of your program uses two classes, RandomArt and RandomArtApplet. You should compile both RandomArt.java and RandomArtApplet.java, to get the .class files RandomArt.class and RandomArtApplet.class. Then you should copy the three files cs124.html, RandomArt.class and RandomArtApplet.class into your www folder. As soon as you do this, your new Web page can be viewed in any browser by using a location of the form
http://math.hws.edu/~YOUR_USER_NAME/cs124.html
where you need to replace YOUR_USER_NAME with your own user name (such as ab1234). The character before the user name is a squiggle, which you can find on the upper left key of the keyboard.
I will check that you have posted your work on the Web. If you have any trouble doing it, be sure to ask for help!
(Note that the use of applets on the web is becoming less and less common and less supported by web browsers. Some web browsers might not show your applet.)
In Case You're Interested: Adding Your First Drawing to the Web
If you edit the file cs124.html, you'll see this line, which is what actually includes the applet onto the web page:
<p><applet code="RandomArtApplet.class" width="800" height="600"></applet></p>
Note that it is RandomArtApplet.class that creates the applet. That class loads the actual art program, RandomArt.class, so both class files must be present for the applet to work.
You might want to add your drawing applet from Lab 2 to your web page. This is not a required part of this lab. To do that, you need to create an applet class file, similar to RandomArtApplet.java. Here are the steps:
- Copy the file RandomArtApplet.java to a new file named FirstGraphicsApplet.java.
- Edit the file FirstGraphicsApplet.java. Change the name of the class from RandomArtApplet to FirstGraphicsApplet. Where the original file says "RandomArt", change that to FirstGraphics. (If you renamed the FirstGraphics program, then wherever I mention FirstGraphics here, you should use the name that you actually used for the program.)
- Copy the file FirstGraphics.java into the same directory that contains FirstGraphicsApplet.java
- Compile FirstGraphicsApplet.java and FirstGraphics.java to get the class files FirstGraphics.class and FirstGraphicsApplet.class.
- Copy the class files FirstGraphics.class and FirstGraphicsApplet.class to your www folder.
- Edit the copy of the file cs124.html that is in your www folder. Add the following line to that file:
<p><applet code="FirstGraphicsApplet.class" width="800" height="600"></applet></p>
You will probably also want to add some text to the page to describe the applet. You can imitate what was done for the RandomArtApplet. When you reload the web page in a web browser, your FirstGraphics applet should be there.
If you would like some basic information about creating web pages, see Section 6.2 in the textbook.