Question: In java: How do I intergrate my ADT HashTable with my Phonebook class? In other words. What function names will I need to change or
In java: How do I intergrate my ADT HashTable with my Phonebook class? In other words. What function names will I need to change or apply to my program in order for my Phonebook class and HashTable to pass values with each other?
Info:
PhoneBook, which represents the phone book. The class should contain a hash table as a datafield. This table contains the people in the book. The phone book should contain the add,delete, find, change, quit, save and restore methods.
HashTable, which is the ADT HashTable. This is the class which contains the PhoneBooks collection of data (all of the People objects in the PhoneBook), as well as the operations which can be performed on that collection of data.
My PhoneBook class:
public class People {
String Name; Integer Phone; People(int Phone,String Name ){ this.Phone=Phone; this.Name=Name; } public String getName() { return Name; } public void setName(String name) { Name = name; } public int getPhone() { return Phone; } public void setPhone(int phone) { Phone = phone; } }
Then create the Phone book class, which is going to use the above People class as value and phone number as the key in the HashMap entries.
import java.security.KeyStore.Entry; import java.util.HashMap;
public class PhoneBook { HashMap
}
My HashTable class:
class HashNode
// Reference to next node HashNode
// Constructor public HashNode(K key, V value) { this.key = key; this.value = value; } }
// Class to represent entire hash table class Map
// Current capacity of array list private int numBuckets;
// Current size of array list private int size;
// Constructor (Initializes capacity, size and // empty chains. public Map() { bucketArray = new ArrayList<>(); numBuckets = 10; size = 0;
// Create empty chains for (int i = 0; i < numBuckets; i++) bucketArray.add(null); }
public int size() { return size; } public boolean isEmpty() { return size() == 0; }
// This implements hash function to find index // for a key private int getBucketIndex(K key) { int hashCode = key.hashCode(); int index = hashCode % numBuckets; return index; }
// Method to remove a given key public V remove(K key) { // Apply hash function to find index for given key int bucketIndex = getBucketIndex(key);
// Get head of chain HashNode
// Search for key in its chain HashNode
// Else keep moving in chain prev = head; head = head.next; }
// If key was not there if (head == null) return null;
// Reduce size size--;
// Remove key if (prev != null) prev.next = head.next; else bucketArray.set(bucketIndex, head.next);
return head.value; }
// Returns value for a key public V get(K key) { // Find head of chain for given key int bucketIndex = getBucketIndex(key); HashNode
// Search key in chain while (head != null) { if (head.key.equals(key)) return head.value; head = head.next; }
// If key not found return null; }
// Adds a key value pair to hash public void add(K key, V value) { // Find head of chain for given key int bucketIndex = getBucketIndex(key); HashNode
// Check if key is already present while (head != null) { if (head.key.equals(key)) { head.value = value; return; } head = head.next; }
// Insert key in chain size++; head = bucketArray.get(bucketIndex); HashNode
// If load factor goes beyond threshold, then // double hash table size if ((1.0*size)/numBuckets >= 0.7) { ArrayList
for (HashNode
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
