Java Input/Output  «Prev  Next»

Read Data FileReader - Exercise

Displaying facts read from file

Objective: Create an applet that uses a file reader to read facts from a text file and randomly display them.
Instructions
In the next module you will complete a client/server application that asks you questions in a "fact or fiction" format. The server side of the application will read the questions and answers from a text file named Factoids.txt.
In this exercise, you prepare for the later part of the project by creating an applet named Factoid that reads factoids from a text file named Factoids.txt and randomly displays them one at a time. You can think of the Factoid applet as a means of studying for the questions asked in the client/server application.
In general, applets running in Java-enabled browsers are not permitted to read or write files. As a result, you may only be able to run the Factoid applet using appletviewer.
Here are the development guidelines for the Factoid applet:
The factoids are listed as individual lines of text in the file Factoids.txt. The Factoids.txt file for this exercise can be found in the 06-09 folder. The Factoid applet should read all of the factoids in the file and then randomly display one of them.
It should then use a thread to select and display another random factoid every second.
Because there can be any number of factoids in the Factoids.txt file. it is helpful to use the Vector class to store the factoids. The Vector class is in the package java.util and implements an array of objects that grows as needed. To add elements to a Vector you can use addElement() and to retrieve the element at a specific index you can use elementAt(). The following example demonstrates using the Vector class to store some strings and then retrieving a randomly selected element.
// Create a vector
Vector v = new Vector();

// Add some strings
v.addElement("Apple");
v.addElement("Peach");
v.addElement("Banana");
v.addElement("Orange");

// Retrieve a random element
int index = (int)(Math.random() * v.size());
String s = (String)v.elementAt(index);

Source files

Factoids.txt
A teaspoon inserted into an open bottle of champagne will not prevent the wine from going flat. Eating carrots will not improve night vision. Fish guts are added to some English beers to help improve clarity. Dogs and cats are born in amniotic sacs, and therefore have no belly button. A 90-pound chimpanzee is capable of hurling a full-grown man through the air. All mammals have tongues. Rats have been known to swim through toilet piping and emerge in a toilet bowl. It is possible to swallow water while standing on your head. The suicide rate does not increase during the winter holiday season. Rice thrown at weddings isn't bad for birds who might eat it.

Hints

  1. In the init() method of your applet, read the factoids from the file Factoids.txt and store them in a Vector.
  2. Your solution will require a separate thread of execution to randomly display a new factoid every second. To accomplish this, your applet should implement the Runnable interface and provide a run() method. The run() method should consist of an infinite loop that selects a factoid at random, invokes repaint(), and pauses for one second.
  3. Override the start() method of your applet to create a thread using your applet as the thread's target and then start the thread.
  4. Override the paint() method of your applet to invoke drawString() with the currently selected factoid.

Exercise scoring

The full credit for this exercise is 15 points. To receive full credit, you will need to successfully create the applet. You will submit your source code.

What to submit

In the text box below, cut and paste the source code for the Factoid applet. Click the Submit button to submit the code