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 phonebook=new HashMap(); HashMap delete_phonebook=new HashMap(); //For restoring the deleted entries PhoneBook(){ // Adding sample data phonebook.put(57371,new People(57371,"Jack")); phonebook.put(57431,new People(57431,"John")); phonebook.put(57577,new People(57577,"Ronny")); phonebook.put(57583,new People(57583,"Johnny")); phonebook.put(51577,new People(51577,"Samuel")); phonebook.put(24177,new People(24177,"Nathan")); } public void add(Integer num,People p ){ phonebook.put(num,new People(num,"Ronny")); } public void save(Integer num, People p){ phonebook.put(num, p); } //Delete by number public void delete(Integer num){ //Storing the entry first in delete_phonebook People temp=this.find(num); delete_phonebook.put(num, temp); phonebook.remove(num); } // Delete by Name public void delete(String name){ People temp=this.find(name); // Searching by name in the below mthod Integer delPhone=temp.getPhone(); delete(delPhone); //Calling the above delete method for removal } //Different types of find method, this one based on phone number as input key public People find(Integer num){ return phonebook.get(num); } //Find mrthod based on Name public People find(String name){ for(java.util.Map.Entry entry : phonebook.entrySet()){ if(entry.getValue().getName().equals(name)){ return entry.getValue(); } } return null; } public void changeName(Integer num,String newName){ People temp=this.find(num); temp.setName(newName); phonebook.remove(num); // Removing the old entry phonebook.put(num,temp); //Saving the new entry with updated Name } public void changeNumber(Integer newnum,String name){ People temp=this.find(name); Integer oldNum=temp.getPhone(); temp.setPhone(newnum); phonebook.remove(oldNum); // Removing the old entry phonebook.put(newnum,temp); } public void restore(String name){ for(java.util.Map.Entry entry : delete_phonebook.entrySet()){ if(entry.getValue().getName().equals(name)){ Integer delnum=entry.getValue().getPhone(); People temp=delete_phonebook.get(delnum); Integer res_num=temp.getPhone(); phonebook.put(res_num, temp); } } }

}

My HashTable class:

class HashNode { K key; V value;

// Reference to next node HashNode next;

// Constructor public HashNode(K key, V value) { this.key = key; this.value = value; } }

// Class to represent entire hash table class Map { // bucketArray is used to store array of chains private ArrayList> bucketArray;

// 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 head = bucketArray.get(bucketIndex);

// Search for key in its chain HashNode prev = null; while (head != null) { // If Key found if (head.key.equals(key)) break;

// 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 head = bucketArray.get(bucketIndex);

// 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 head = bucketArray.get(bucketIndex);

// 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 newNode = new HashNode(key, value); newNode.next = head; bucketArray.set(bucketIndex, newNode);

// If load factor goes beyond threshold, then // double hash table size if ((1.0*size)/numBuckets >= 0.7) { ArrayList> temp = bucketArray; bucketArray = new ArrayList<>(); numBuckets = 2 * numBuckets; size = 0; for (int i = 0; i < numBuckets; i++) bucketArray.add(null);

for (HashNode headNode : temp) { while (headNode != null) { add(headNode.key, headNode.value); headNode = headNode.next; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!