Computer Science 241
Assignment 1
Due: January 31, 2002

First create a text file with four lines.  The first should be your name, the second your social security number, then your e-mail address and finally your telephone number.  Save this on your disk.

Next write a Java applet to read this information into a class and then display it in text boxes.  Your program should have at least two classes.  The first one is the main class that extends the Applet class.  It should create TextFields and Labels for each of the data items in the file.  Then it should instantiate the second class and call methods in it.

The data in the second class should correspond with the data in the file.  It should have at least two methods, one to read the data into the class and the other to display the data in the text boxes on the applet.

Hand in a printout of your program together with a disk with the file and the .java, .class and .html files.  You can use the program below to guide you in writing your program.

// Computer Science 241
// <name>
// <date>

import java.awt.*;
import java.applet.Applet;
import java.io.*;

// An application with a class that displays data read from a file in text boxes on an applet.
public class People extends Applet
{
     private TextField name, age;
     private Label lblName, lblAge;
 
     public void init ()
     {
          lblName = new Label ("Name");  name = new TextField (20);
          lblAge = new Label ("Age"); age = new TextField (10);
          add (lblName); add (name);
          add (lblAge); add (age);
          Person person = new Person ();
          person.readData ();
          person.displayData (name, age);
     } // init method
} // class People
 
// Class that stores information about a person's name and age.
class Person
{
     private String name;
     private int age;
 
     // A method to read data from a file.  It uses a buffered reader.
     public void readData ()
     {
           try
          {
                BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
                name = infile.readLine ();
                age = Integer.parseInt (infile.readLine ());
          }
          catch (IOException e) { System.out.println ("I/O error");}
          catch (NumberFormatException e) { System.out.println ("Number format error.");}
     } // method readData
 
     // A method that displays data in text boxes on the applet.
     public void displayData (TextField n, TextField a)
     {
          n.setText (name);
          a.setText ("" + age);
     } // method displayData
} // class Person