![]() |
The Hashtable Class
PrerequisitesIn 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 LocationPerform 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:
Replace the array with a Hashtable in the Company class. Use Employee as both the key and value. Rewrite the findEmployee method to use the Hashtable for the search. Rewrite the findEmployeeNamed method to enumerate through the Hashtable to do the search. Rewrite the toString method of the Company class to use the Hashtable. Run the program via the HashTest class.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |