TelephoneBook book = new TelephoneBook(); // create object with TelephoneBook class book.insert("NAMI","123-4567"); // use object book to call insert() method
PersonNode
class should look like this:
class PersonNode{ //data field for name //data field for telephone number //data field for next PersonNode //constructor //toString() method //getName() method //getTelephoneNumber() method //getNext() method //setNext() method }
LinkedList
class should look like this:
class LinkedList{ //data field for PersonNode head //constructor //toString() method //add() method //get() method //remove() method }
class TelephoneBook{ //array of 7 LinkedList elements //constructor //insert() method //retrieve() method //delete() method //toString() method }
h(key) = key.hashCode() % 7
.
Below is code that shows how to use method hashCode() and modulus (%) to calculate the hash function.
The "key" of the hash function is the name of the person.
You should use the hash function in the insert(), retrieve(), and delete() methods to locate the array index of the element.
Integer stringHash = "BUBBA".hashCode(); // stringHash is 63550158 Integer myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0] stringHash = "SUE".hashCode(); // stringHash is 82467 myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0] stringHash = "CAT".hashCode(); // stringHash is 66486 myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0]
Reading from input file: input-project.csv Personal Telephone Book Hash Table MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 4 DISPLAY TABLE table[0] => null table[1] => LEE 777-7777 => BAKER 111-1111 => null table[2] => KAM 654-7890 => LONG 987-1234 => DAVIS 999-9999 => null table[3] => FOX 666-6666 => null table[4] => CASTRO 888-8888 => CARTER 222-2222 => null table[5] => KING 555-5555 => HALL 123-4566 => null table[6] => null MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 1 Enter person's name: BUBBA Enter person's telephone number: 111-2222 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 1 Enter person's name: SUE Enter person's telephone number: 222-3333 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 1 Enter person's name: CAT Enter person's telephone number: 555-7777 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 4 DISPLAY TABLE table[0] => CAT 555-7777 => SUE 222-3333 => BUBBA 111-2222 => null table[1] => LEE 777-7777 => BAKER 111-1111 => null table[2] => KAM 654-7890 => LONG 987-1234 => DAVIS 999-9999 => null table[3] => FOX 666-6666 => null table[4] => CASTRO 888-8888 => CARTER 222-2222 => null table[5] => KING 555-5555 => HALL 123-4566 => null table[6] => null MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 2 Enter person's name: LEE The telephone number is: 777-7777 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 2 Enter person's name: DAVIS The telephone number is: 999-9999 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 3 Enter person's name: LEE Deleting: LEE 777-7777 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 3 Enter person's name: LONG Deleting: LONG 987-1234 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 3 Enter person's name: HALL Deleting: HALL 123-4566 MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 4 DISPLAY TABLE table[0] => CAT 555-7777 => SUE 222-3333 => BUBBA 111-2222 => null table[1] => BAKER 111-1111 => null table[2] => KAM 654-7890 => DAVIS 999-9999 => null table[3] => FOX 666-6666 => null table[4] => CASTRO 888-8888 => CARTER 222-2222 => null table[5] => KING 555-5555 => null table[6] => null MAIN MENU 1. Insert telephone number 2. Retrieve telephone number 3. Delete telephone number 4. Display telephone book 5. End program Enter choice (1-5): 5 Wrote to output file: output-project.csv
Click to validate the HTML code
Click to validate the CSS code