Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java: How do I intergrate my ADT HashTable with my Phonebook class? In other

ID: 3843725 • Letter: I

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 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 PhoneBook’s 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<Integer,People> phonebook=new HashMap<Integer,People>();
  
  
HashMap<Integer,People> delete_phonebook=new HashMap<Integer,People>(); //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<Integer, People> 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<Integer, People> 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, V>
{
K key;
V value;

// Reference to next node
HashNode<K, V> next;

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

// Class to represent entire hash table
class Map<K, V>
{
// bucketArray is used to store array of chains
private ArrayList<HashNode<K, V>> 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<K, V> head = bucketArray.get(bucketIndex);

// Search for key in its chain
HashNode<K, V> 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<K, V> 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<K, V> 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<K, V> newNode = new HashNode<K, V>(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<HashNode<K, V>> temp = bucketArray;
bucketArray = new ArrayList<>();
numBuckets = 2 * numBuckets;
size = 0;
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);

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

Explanation / Answer

Solution :

PhoneBook class looks like this after editing :


package javaapplication12;


public class PhoneBook {
  
Map<Integer,People> phonebook=new Map<Integer,People>();
  
  
Map<Integer,People> delete_phonebook=new Map<Integer,People>(); //For restoring the deleted entries
PhoneBook(){
  
// Adding sample data
phonebook.add(57371,new People(57371,"Jack"));
phonebook.add(57431,new People(57431,"John"));
phonebook.add(57577,new People(57577,"Ronny"));
phonebook.add(57583,new People(57583,"Johnny"));
phonebook.add(51577,new People(51577,"Samuel"));
phonebook.add(24177,new People(24177,"Nathan"));
  
}
  
public void add(Integer num,People p ){
phonebook.add(num,p);
}
  
public void save(Integer num, People p){
phonebook.add(num, p);
}
  
//Delete by number
public void delete(Integer num){
//Storing the entry first in delete_phonebook
People temp=this.find(num);
delete_phonebook.add(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){
HashNode<Integer, People> head = null;
for(int i =0; i<phonebook.numBuckets; i++){ // check for each bucket in hashmap
head = phonebook.bucketArray.get(i);

while(head != null){ // check for each hash node in given bucket.
if(head.value.Name == name){
return head.value;
}
head= head.next;
}
}
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.add(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.add(newnum,temp);
}
  
public void restore(String name){
People p = null;
HashNode<Integer, People> head = null;
for(int i =0; i<delete_phonebook.numBuckets; i++){ // check for each bucket in hashmap
head = delete_phonebook.bucketArray.get(i);

while(head != null){ // check for each hash node in given bucket.
if(head.value.Name == name){
p = head.value;
}
head= head.next;
}
}

phonebook.add(p.Phone, p);
  
}
}

Map class :


package javaapplication12;

import java.util.ArrayList;

class Map<K, V>
{
// bucketArray is used to store array of chains
public ArrayList<HashNode<K, V>> bucketArray;
// Current capacity of array list
public int numBuckets;
// Current size of array list
public 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<K, V> head = bucketArray.get(bucketIndex);
// Search for key in its chain
HashNode<K, V> 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<K, V> 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<K, V> 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<K, V> newNode = new HashNode<K, V>(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<HashNode<K, V>> temp = bucketArray;
bucketArray = new ArrayList<>();
numBuckets = 2 * numBuckets;
size = 0;
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);
for (HashNode<K, V> headNode : temp)
{
while (headNode != null)
{
add(headNode.key, headNode.value);
headNode = headNode.next;
}
}
}
}
}

Note : i have not made any other changes in any other class.

Only PhoneBook class is updated and variables of Map class are changed to public from private.

No other changes are there.

If you have any doubts, you can ask in comment section.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote