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

Assigned:WED NOV 24
Due: THUR DEC 9, 11:59pm. No projects will be accepted after that and there will be no resubmissions.
Final Exam:Sections 002 & 003 MON DEC 20 2:00-3:50

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 270 in the text).

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

  1. Write a method public static void formArray(String name, DataBase[] dv) that calls formTokens 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 Prog5 of November 24 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 formTokens(String line, DataBase data). The string line is read in formArray (see above) from the database file. The method formTokens 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 printThem(DataBase[] dv) calls public static void printIt(DataBase data) which prints the fields of an element of dv, as we did in Prog5 of November 24.
  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 studied in class. (See the Prog3.java on the Nov 22 class link) 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
WED NOV 22, 22:23:14 EST 2004