Regarding Keys and class Database

When you read the code for class Database, you will see that class Database does not manufacture any keys. Indeed, when a Record is inserted into the Database, the Record holds inside itself already a Key.

Later, when a user wishes to find a Record in the Database, the user must supply the Key of the Record that is desired. The find method invokes locationOf, which uses the Key to examine all the Records in the Database until the Record that holds the same Key is located.

Most importantly, the index numbers of the array inside class Database have nothing to do with the Keys that are held inside the Records. To see this, consider this example:

Remember from an earlier demo program that we have a class named BasicPerson, which models a Record that remembers info about a person. Here it is again:

package TestTheDatabase;
import Database.*;  // remember to import the Database package

/** BasicPerson models a simple person that knows only its name and key  */
public class BasicPerson implements Record
{ private Key k;
  private String who;

  /** BasicPerson constructs the person's record
    * @param name - the person's name
    * @param j - the person's key  */
  public BasicPerson(String name, Key j)
  { who = name;
    k = j;
  }

  /** nameOf returns the person's name  */
  public String nameOf()
  { return who; }

  public Key keyOf()
  { return k; }
}
Now, say that each BasicPerson is constructed so that the Key it holds inside itself is a string of 3 letters. To do this, we write this class, named StringKey:
package TestTheDatabase;
import Database.*;    // remember to import the Database package

/** StringKey models a key that is a string  */
public class StringKey implements Key
{ private String id;  // the key

  /** StringKey  constructs the key object
    * @param j - the key */
  public StringKey(String j)
  { id = j; }

  /** valOf  returns the value of the key */
  public String valOf()
  { return id; }

  public boolean equals(Key another_key)
  { // since  another_key  is also meant to be a  StringKey,  cast it to
    //  type  StringKey  and use its  valOf  method to get a String:
    String m = ((StringKey)another_key).valOf();
    return  id.equals(m);
  }

  public boolean lessthan(Key another_key)
  { String m = ((StringKey)another_key).valOf();
    // the  compareTo  method returns a negative int if the first string
    // is lessthan the second (according to alphabetical ordering):
    return  (id.compareTo(m) < 0);
  }
}

Now, let's construct two keys and two persons:

StringKey k1 = new StringKey("ABC");
StringKey k2 = new StringKey("XYZ");
BasicPerson p1 = new BasicPerson("Fred Mertz, NYC", k1);
BasicPerson p2 = new BasicPerson("Lucy Ricardo, New Rochelle, NY", k2);
The StringKeys are installed inside the BasicPerson objects.

Next, let's construct a Database and insert the two persons:

Database db = new Database(5);  // can hold 5 Records
db.insert(p2);
db.insert(p1);
The Database, db, stores the records in its internal array. The user of the database knows nothing about which cells of the array hold the two records.

Now, let's say we wish to find the Record whose Key is k2; do this:

Record r = db.find(k2);
This statement returns the address of the record named p2 and assign it to r. Again, we know nothing about array indexes; we work only with the StringKeys.

Now you should be able to understand why the Database object can move around, sort, and delete the records internally in its array, and the user of the Database sees none of this.