The Hashtable Class


Prerequisites

In this Magercise a Hashtable is used to replace a Company class' array of employees. The Enumeration interface will be used to re-implement the findEmployeeNamed method.

The JDK provides two useful container classes, Vector and Hashtable, that provide more collection capabilites that arrays, but with the disadvanage of losing some type information: Any method of Vector or Hashtable that returns an element of that collection returns an Object. This Object must then be downcast to the desired type, for example:

anEmployee = (Employee)someVector.firstElement();

In this case it is known that the Hashtable in use will only have Employee objects in it, so the unchecked downcast is safe. A later Magercise will show how to determine the actual type of an object before downcasting.

The semantics of Java related to pointers and assignment are discussed in detail in the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.The Hashtable Class.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Add statements to import the new classes to be used.

  2. Replace the array with a Hashtable in the Company class. Use Employee as both the key and value.

  3. Rewrite the findEmployee method to use the Hashtable for the search.

  4. Rewrite the findEmployeeNamed method to enumerate through the Hashtable to do the search.

  5. Rewrite the toString method of the Company class to use the Hashtable.

  6. Run the program via the HashTest class.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.