CS111, Wellesley College

Lab 12

Wedn, Nov 28 2007

Custom-made Java Objects & File I/O

The goals of this lab is to give you practice with:

In particular, we will read information about several students from a file -provided to us-, then we will filter these data according to some criteria -by lecture section, as an example- and finaly write the results back to a file.

You will develop this application mostly from scratch. There is very little helping code prepared for you. However, do download the folder lab12_programs from the download directory. You will find there a file containing some input data, and a starting version for the WellesleyStudents.java program.

Student class

We will define a Java class, named Student, that will allow us to store information about a single student. For this application, we are interested in the following piece of data, related to a student instance:
  1. last name,
  2. first name,
  3. year the student belongs to,
  4. her cs230 lecture section, and
  5. her lab section.
All of the above pieces of information will be represented as strings.

Instance Variables

What instance variables do we need to have in the Student class?

Constructor(s)

We will provide two constructors for this class:

Instance Methods

It is usually a good practice to make the instance variables private to the class. In that case, the class implementer should provide "getter methods", so information about a particular instance of the class can be accessed from outside the class. In particular, we will define the following public instance methods:
It is always a very good idea to implement the toString() instance method, which returns a String representation of the invoking object instance. This method is very useful for testing purposes, and not only. We recommend that you implement the toString() method every time you define a class! For example, here is a printed representation of an instance of the Student class:
Soo Been Kim year 2010 is in lec02 and lab03

Add a main() method to your Student.java file, so you can test your code as you develop it. As always, you should test your code incrementally, as you add it to the Student.java file.

WellesleyStudents class

The purpose of this class is to read information about a group of Student objects from a file, process and manipulate the data according to the user's needs, and write the results out to an output file.

Instance Variables

In this implementation we will use an array to hold the Student instances. Could you think of any other structure that we could use instead? Add the following two instance variables to your class:

  Student[] students; //array to hold the Student instances
  int numRecords = 32; //number of objects it will hold
  

Constructor

The constructor creates the instance variable array, students, and populates it with Student instances. The data to populate the array will be read from a file in memory, which is passed as an argument to the constructor. The definition of the constructor is provided. However, this definition, invokes the instance method fromFileToArray(), which you are asked to implement. Before you try to implement this method, you may want to study the definition of the method fromFileToScreen() shown below.

Instance Methods

Read from a File

The method definition fromFileToScreen() is given to you below. Study this definition, to refresh yourself on some of the details on how to read a file in Java.

  public void fromFileToScreen(String inFile) {
    try {
      BufferedReader reader = new BufferedReader(new FileReader(inFile));
      String line = reader.readLine(); //read the first line of the file
      while (line != null) {  //line becomes null at end of file
        System.out.println(line);
        line = reader.readLine(); //read the next line of the file
      }
      reader.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
  

Printing an Array

The method shown here prints the contents of the input array in the console. Notice that only non-null objects are printed, and how the toString() method, in the Student class, is implicitly used.

    public void printArray(Student[] a) {
    for (int i=0; i < a.length; i++) {
      if (a[i] != null)
        System.out.println(a[i]);  //will be using the toString() on Student
    }
  }