Home CPSC 240

Lab 10: Hash Tables

 

Objective

To get experience using the Hash Table data structure.


 

Task

For this program, you will write a program for storing people's phone numbers. The program will be similar to the "Contacts" program on your phone. It will allow the user to add a contact, or lookup a contact's phone number.

To do this, you will create a Hashtable which maps Strings (the contact name) onto Strings (the contact phone number).

You will then write a program which contains a menu. The menu should have three options:

  1. Add a contact: This will ask for the name and phone number and add it to the table.
  2. Search for a contact: This will ask for the name of a contact and display the phone number. If the name doesn't exist, you should print a message to that effect.
  3. Quit: this should break out of the loop and exit the program.

 

Example Run

(A)dd a contact, (S)earch for a contact, or (Q)uit?
: A
Enter name: Joe
Enter number: 555-5555
(A)dd a contact, (S)earch for a contact, or (Q)uit?
: A
Enter name: Sue
Enter number: 123-4567
(A)dd a contact, (S)earch for a contact, or (Q)uit?
: S
Enter name: Joe
555-5555
(A)dd a contact, (S)earch for a contact, or (Q)uit?
: S
Enter name: Bob
No contact found
(A)dd a contact, (S)earch for a contact, or (Q)uit?
: Q 

 

Submitting

When you are done, please email your .java file to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.