Expected time required: 10 min
In this exercise you will add functionality to the ParticipantDirectory
example to be able to:
- look up participants by ID number and
- obtain a roster of participants sorted by ID number.
This extra functionality will be accomplished by adding a second
TreeMap to the ParticipantDirectory class.
Remember, a TreeMap associates a key with an object. The keys are
stored in a balanced binary search tree based on the sorted order of the
keys and provides efficient access using the keys. The values that are
associated with each key in a TreeMap are actually object references.
Multiple TreeMaps using different keys do not require multiple
copies of the objects. Thus, multiple TreeMaps can provide multiple ways
of efficiently accessing and organizing the same set of objects.
Figure 1 shows an example with three objects that each contain a character
attribute and an integer attribute. The objects are A/2, B/3, and C/1.
One TreeMap using the character attribute as the keys is shown on the left
with B as the root, A as the left child, and C as the right child.
A second TreeMap using the integer attribute as the keys is shown in the
right with 2 as the root, 1 as the left child, and 3 as the right child.
Figure 1: Example showing multiple TreeMaps indexing the same objects.
Before we begin, examine the provided code and note the differences from the code as it was presented in your reading:
-
The
ParticipantDirectoryclass has anaddParticipantmethod that acceptsStringparameters for the participant's username and full name. This method then creates a newParticipantobject and stores it in theTreeMap. -
The previous
printRosterByUsernamemethod has been re-written asgetRosterByUsernameand instead of printing directly toSystem.out, it builds and returns aStringwith the participant roster. This will allow for more flexible use of the roster information by a client application. (That is, it may still be printed to the console, or it could be displayed in a GUI component, or it may be sent directly to a file, to name just a few possibilities.) -
Some new test data is now loaded in the
mainmethod usingaddParticipant.
Note: Completing this exercise does not require making any
changes to the Participant class.
Your task for this exercise is as follows:
-
Add a second
TreeMapto theParticipantDirectoryclass to mapIntegerkeys toParticipantvalues.A) Declare the second
TreeMapas an instance variable, just under the declaration of theparticipantsinstance variable.B) Instantiate the new
TreeMapin theParticipantDirectory()constructor. -
Add a line to the
addParticipantmethod to add the newly createdParticipantobject to the newTreeMapusing itsidNumberas the key. Note: Be sure to only create oneParticipantobject and add that same object to both maps using the appropriate key for each map. -
Complete the
getParticipantByIDmethod that takes anIntegerid number as a parameter and returns theParticipantobject associated with that id number by the newly addedTreeMap. Note: This method will be very similar to thegetParticipantByUsernamemethod. -
Complete the
getRosterByIDmethod that builds and returns a string containing the current roster of participants sorted by ID number. Note: This method will be very similar to thegetRosterByUsernamemethod. -
(Optional) Before running the unit tests, add some code to the
mainmethod in theParticipantDirectoryclass to test your newTreeMapsimilar to the tests ofgetParticipantByUsernameandgetRosterByUsername.
HINTS:
