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

Assigned:MON NOV 28
Due: SUN DEC11, 11:59pm. No projects will be accepted after that and there will be no resubmissions.
Final Exam: MON DEC 19 10:00-11: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 274 in the text).

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

  1. Write a method public static void readArray( DataBase[] dv) that calls readRecord 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. In readArray,each record read is assigned to successive elements of dv. Use the formulation given in Prog2 of November 28 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 readRecord line, DataBase data). The string line is read in readArray (see above) from the database file. The method 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 printArray(DataBase[] dv) calls public static void printRecord(DataBase data) which prints the fields of an element of dv, as we did in Prog2 of November 28.
  4. Finally, write public static void sort(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 Prog6.java on the Nov 21 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.

Your program should sort on a key read by your program. Your program should be robust throughout. If the key is out of range, your program should indicate this. After the sort is done, your program should ask whether the user wants to continue, just as in the craps game. If the user types any input other than y or n, the program should inform him/her of the error and request new input.


Samuel Marateck
WED NOV 22, 22:23:14 EST 2005