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

Assigned: MON NOV 26
Due: TUE DEC 12, 11:59pm. No projects will be accepted after that and there will be no resubmissions.
Final Exam:TUE DEC 19, 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, write what is called a scanner (also called a lexical analyser). This will consist of two methods:

If you want, you may use the StringTokenizer class (see page 246-248 in the text). If so, do not write the two methods just described and skip step (1) in the following.

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

  1. Write a program that determines the column of the first and last character of a token in an inputted string. Then using the substring method, it prints the word.
  2. Write a method public static Database formWords( String record) that forms the tokens from the record and prints them. Database is a class you must write consisting of an array of four fields which are strings.
  3. Alter formWords so that it assigns the four tokens to an instance of the class Database and returns that instance.
  4. Write the method public static Database[] formArray( String file, Database[] array) that enables your program to read the entire file, and stores the instances of Database in elements of an array of type Database[]. The end-of-file corresponds to null so you may detect it in a while loop. Note that you cannot print the elements of array since they are instances of Database and are thus pointers to the heap. You can, however, print the fields of an element of array.
  5. Finally, write public static Database[] 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 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
SUN Nov 26 22:23:14 EST 2000