V22.0101, Homework Assignment 5, Fall 2001
Sorting a Database

Assigned: MON NOV 26
Due: MON DEC 10, 11:59pm. No projects will be accepted after that and there will be no resubmissions.
Final Exam:TUE DEC 18, 2:00-4:50 pm.

In this project you are asked to write a program that sorts the records of a database on a given field. The link to the data base is directly beneath this link. The first few records are:

marin gil     269972639  34
abrams rita   005456788  17
nottobe tobe  029209330  19
backus jim    379228370  23

The first field of the record contains the person's last name; the second field, the first name; the third, the social security numer; and the fourth, the person's age. If the records are sorted on the fourth field, the results would be:

abrams rita   005456788  17   
nottobe tobe  029209330  19      
backus jim    379228370  23  
marin gil     269972639  34  

We will call the entries in the records tokens. One or more blanks separate the tokens from each other. To enable your program to distinguish the tokens in a record, use the StringTokenizer class (see page 246-248 in the text).

Here are the steps you should follow to write the program:

  1. Write a method public static void read1(String name, DataBase[] dv) that calls read to form the tokens from the record. Database is a class given on the homepage consisting of an array of four fields which are strings. Each record read is assigned to successive elements of dv. Use the formulation given in Prog3 of December 3 link. Note that the end-of-file corresponds to null pointer so you may detect it in a while loop.
  2. Write the method public static void read(String line, DataBase data). The string line is read in read1 (see above) from the database file. The method read uses the StringTokenizer to assign the four tokens to an instance of the class Database.
  3. Note that you cannot print the elements of dv since they are instances of Database and are thus pointers to the heap. You can, however, print the fields of an element of dv. We do this in two steps. public static void print_them(DataBase[] dv) calls public static void print(DataBase data) which prints the fields of an element of dv, as we did in Prog5 of December 3.
  4. Finally, write public static void (Database[] array) which sorts the array on a field indicated by the user. It is suggested that you use the bubble sort which we will study in class. (See page 464 in the Turbo Pascal textbook.) To compare two strings, use the compareTo method. If string1 appears later than string2 in a dictionary, then string1.compareTo( string2 ) > 0. If it appears before, then it is < 0.


Samuel Marateck
THURS Nov 22 22:23:14 EST 2001