V22.0101, Homework Assignment # 5
Sorting a Database

Due 12/13 (postponed)!


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

marin gil     269972639  34
abrams rita   005456788  7
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  7   
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.

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

  1. Use TestDatabase.txt to read a file consisting of an array of four fields into your database. Each record read is assigned to successive elements of dv. Notes: Each item is of type Comparable so that the array can be sorted by various types of data. Also, the end-of-file corresponds to null pointer so you may detect it in a while loop.
  2. Here is the FileStringReader.java class needed for the TestDatabase.java (from above) to read file.
  3. 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 sort algorithms which we studied in class; however, you can use a sort algorithm of your choice. To compare two Comparable objects, use the compareTo method. If comparableObject1 appears later than comparableObject2 in a dictionary, then comparableObject1.compareTo( comparableObject2 ) > 0. If it appears before, then it is < 0.