Computer Science 122
Assignment 2
Due: February 5, 2002

Modify your first program so that instead of reading in your personal information, it reads in a check and a deposit.  Create two separate classes for these, one called Check and the other called Deposit.  For now, do not have them inherit from a super class.  We will do that later.

The try and catch block with the declaration for the BufferedReader will now have to go into the main method.  The example below shows how to do this.  Also the readCheck and readDeposit methods will have to have the BufferedReader as a parameter.  Instead of having those methods catch the exception, you can throw the exception back to the main class and catch it there.

Store data for the check and the deposit in a file called checkFile.txt.  You may either create data of your own or use the data listed below.  If you use the data listed below, you have to make sure that your program reads the data in the order that it is listed in the file.  Otherwise you might read the wrong line into a field and get an error.

For now, we will treat dates as Strings in the format mm/dd/yyyy.  That is two digits for the month, two digits for the day and four digits for the year all separated by forward slashes.  If you want to handle the date in a different way, make sure that you include complete comments documenting what you have done.

Hand in a printout of your program and a disk with the .java, .class and .txt files.  Make sure the disk is labeled with your name and class.  I have extra labels if you need one.

checkFile.txt

02/04/2002
101
Keyspan
35.66
01/31/2002
XYZ Company
350.12

// Computer Science 122
import java.io.*;

/* An application with a class that provides information about two people.  It reads their data from a file called infile.txt. */
public class People
{
     public static void main (String [] args)
     {
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
               Person girl = new Person ();
               girl.readData (infile);
               girl.displayData ();
               Person boy = new Person ();
               boy.readData (infile);
               boy.displayData ();
          } catch (IOException e) {System.out.println ("File Error.");}
     } // main 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 (BufferedReader infile) throws IOException
     {
          name = infile.readLine ();
          age = Integer.parseInt (infile.readLine ());
     } // method readData
 
     public void displayData ()
     {
          System.out.println (name + "'s age is " + age);
     } // method displayData
} // class Person