Algorithmic Design II - Lab Assignment 2 | CSCE 146, Assignments of Computer Science

Material Type: Assignment; Professor: Buell; Class: ALGORITHMIC DESIGN II; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-el2
koofers-user-el2 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Lab Assignment 2
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.
pf3
pf4
pf5

Partial preview of the text

Download Algorithmic Design II - Lab Assignment 2 | CSCE 146 and more Assignments Computer Science in PDF only on Docsity!

Lab Assignment 2

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; /***********************************************************

  • Class for extending a ’Record’ to include a key. **/ public interface IRecordKeyed { /***********************************************************
  • Method to get the ’key’.
  • @return the ’String’ value of ’key’. **/ public String getKey();

/***********************************************************

  • Method to set the ’key’.
  • @param what the ’String’ value of ’key’ to be set. **/ public void setKey(String what);

/*********************************************************************

  • Method to compare the ’key’ values of ’this’ record against
  • the ’searchString’ provided.
  • @param what the ’searchString’ to compare against.
  • @return -1, 0, or +1 according as the comparison goes. **/ public int compareRecordByKey(String what);

/***********************************************************

  • Method to read the record and set the key.
  • @param inFile the ’Scanner’ from which to read.
  • @return ’RecordKeyed’ record that was read. **/ public RecordKeyed readRecordKeyed(Scanner inFile);

} // 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.

/*************************************************

  • Method to read the record and the key.
  • @param inFile the ’Scanner’ from which to read. **/ public RecordKeyed readRecordKeyed(Scanner inFile) { this.readRecord(inFile); this.setKey(this.getName()); return this; } // public RecordKeyed readRecordKeyed(Scanner inFile)

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

Metaphysics and Commentary

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.