Assignment 3

CS 46B
due March 5, 2003
60 points

Define two public classes StudentFile and StudentRecord that can be used for a simple student record system. Instances of the StudentRecord are to represent students, providing information about their student id number, and their name. Their first (given) and last (family) names should be stored separately. This class will need a constructor with taking the first name, last name, and id number in that order. The id number may be represented as an int rather than as a long.

It will also be necessary to print instances of the StudentRecord class, and sort records of this class. For printing, you should provide either a toString method, or accessors for the private instance variables, or both. Sorting is most easily accomplished by using the sort method of the Collections package, and either the Comparable interface or the Comparator interface, or both. If you use the Comparable interface, you will need to have your class implement this interface, and you will need to provide a compareTo method.

The StudentFile class is to maintain a collection of student records. It is to have public methods add, traverseByName, and traverseByNumber. The add method is to take a StudentRecord as argument, and add it to the StudentFile if it is not null. The method needn't check for insertion of duplicate records. In the null case, you should print a simple error message.

Both the traverseByName, and traverseByNumber are to print representations of StudentRecords to System.out, each on a separate line. In the former method, the last name field is to be the most significant, followed by first name, and id number in that order. In the latter method, the id number is to be the most significant field, followed by the last name and then the first name. This is also the order in which the fields should be printed (for both methods).

Your records needn't be stored in sorted order -- it is acceptable if they are sorted immediately before the file is traversed.

Test your class by executing the test file A3.java, which is provided on the class web site.

A more realistic version of this simple student record system (e.g., to record information about courses taken by a student) is the likely subject of Assignment 4.