



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Buell; Class: ALGORITHMIC DESIGN II; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Fall 2008;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Your assignment for Laboratory 2 is to create an ArrayList version of a phone book, to add a key to the extended record, and to write a main program to read in the data and then search the ArrayList to find a record whose key matches an input String to be used as a key. Sample input and output for the program appears below and is on the website. First, you will need to extend the Record class from the IndexedFlatFile to a RecordKeyed class, and in your RecordKeyed class you will need to implement the IRecordKeyed interface. The IRecordKeyed interface appears on the next page. I am giving you the interface so you will know exactly what methods you need to implement in the extended class. Your RecordKeyed class must add a String variable called key (or else we wouldn’t call it a “keyed” record, would we?), an accessor getKey() and a mutator setKey(). You will also need to have methods
compareRecordByKey(String what)
and
readRecordKeyed(Scanner inFile)
The first of these will run compares the String value of the input parameter against the Key value in the this record and return a boolean true or false depending on whether the match succeeds. The second method will read the original record and then set the key equal to the name.
Figure 1 The interface IRecordKeyed import java.util.Scanner; /***********************************************************
/***********************************************************
/*********************************************************************
/***********************************************************
} // public interface IRecordKeyed
The search through the list looking for a match should be done with code like the following.
for(int i = 0; i < theList.size(); ++i) { if(0 == theList.get(i).compareRecordByKey(searchString)) { // we get a match of keys } else { // we get no match of keys } }
Finally, the readRecordKeyed method will read the original record and then set the key equal to the name. This can be done with the code below.
/*************************************************
Correct output data for this input data exists off the website and appears below. Note that after the stream of add commands is finished, you are to print out the ArrayList as below. the initial number comes from the sequence num- ber of the loop that is walking through the ArrayList. The part in parentheses indicates the key value, with the key inside the single quotes, left adjusted. The stuff beyond the closing parenthesis is the actual record data.
Begin execution The list of 10 items is 0: (key ’Herbert ’) Herbert 2A41 789.0123 390 1: (key ’Lander ’) Lander 2A47 789.7890 146 2: (key ’Winthrop ’) Winthrop 3A71 789.4667 611 3: (key ’Marion ’) Marion 2A13 789.1536 750 4: (key ’Adams ’) Adams 3A56 789.8702 522 5: (key ’Smith ’) Smith 2A46 789.5275 101 6: (key ’Axolotl ’) Axolotl 3A22 789.2749 102 7: (key ’Zokni ’) Zokni 3A39 789.9111 350 8: (key ’Igen ’) Igen 2A21 789.6050 240 9: (key ’Nem ’) Nem 2A89 789.3383 790
Read search string ’Marion’ The keyed record matches
Read search string ’Buell’ The key does not match any record
Read search string ’Smith’ The keyed record matches
No more data, terminate
Note that the main reason for extending the Record code is so that the base Record is independent of the key. It will only be in the extended class that any knowledge will exist about what kind of key is being used or which field in the record is being used as the primary key. In this simple case, both the main program and the extended class know that the key value is a String variable and are able to manipulate the key as such. We will take care of this in the next lab assignment by creating a separate Key class and hiding all the information about the key variable in that class.