Question: The starter code contains HashNode.java, MyHashTable.java, and MyHashTableTester.java. Complete MyHashTable .java; Implement your code in places marked as TODO. MyHashTable class has three instance variables:

The starter code contains HashNode.java, MyHashTable.java, and

MyHashTableTester.java. Complete MyHashTable .java; Implement

your code in places marked as TODO.

MyHashTable class has three instance variables:

bucketArray is an ArrayList that contains objects of type HashNode,

numbBuckets, shows the size of the array list ,

size, shows the number of HashNode objects in the array list.

Note that the number of HashNode objects can be different from the size of the array

list! How is that possible?

MyHashTable class has a constructor that initializes all three instant variables.

While initializing, add null to the empty bucketArray with size 10.

There is getBucketIndex method that is already implemented. You shouldnt

change it:

public int getBucketIndex(K key): Finds the index for the given key. It uses

JVM hashCode() method to generate the hashCode for the given key.

Complete MyHashTable.java by providing the following methods, as described by

the comments in the starter file:

public int getSize(): returns the number of the nodes in the hash table.

public V get(K key): returns the value corresponding to each key if the key

is present and null if no key found.

public void add(K key, V value): Adds new (key, value) pair to the hash

table. If key already presents, it updates the value.

public V remove(K key): removes the (key, value) pair.

public boolean isEmpty(): returns true if the size is zero.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

HashNode.java

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package hashtable;

// A node of chains

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;

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

MyHashTable.java

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package hashtable;

import java.util.ArrayList;

// Class to represent entire hash table

class MyHashTable

{

// bucketArray is used to store array of chains

//TODO

// Current capacity of array list

//TODO

// Current size of array list

//TODO

// Constructor (Initializes capacity, size and

// empty chains.

public MyHashTable()

{

//TODO

}

// Implement the size() method

public int size()

{

//TODO

}

//Implement the isEmpty() method

public boolean isEmpty()

{

//TODO

}

// This implements hash function to find index for a key.

// Do not change this method

private int getBucketIndex(K key)

{

int hashCode = key.hashCode();

int index = hashCode % numBuckets;

return index;

}

// Implement remove() method

// Apply hash function to find index for given key

// Get head of chain

// Search for key in its chain. If Key found remove the node and return the

// value of the node. If key not found return null.

public V remove(K key)

{

//TODO

}

// Implement get() method

// Find head of chain for given key

// Search key in chain. If key found return the value. If not found return null.

public V get(K key)

{

//TODO

}

// Implement add() method

// Find head of chain for given key

// Check if key is already present. If true replace the old value with the new value.

// If false add the new node to the chain and increase the size.

public void add(K key, V value)

{

//TODO

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

MyHashTableTester.java

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package hashtable;

class MyHashTableTester

{

// Driver method to test MyHashTable class

public static void main(String[] args)

{

MyHashTablemap = new MyHashTable<>();

map.add("Harry",1 );

map.add("Nina",2 );

map.add("Harry",4 );

map.add("Tony",5 );

System.out.println(map.size());

System.out.println(map.remove("Harry"));

System.out.println(map.remove("Harry"));

System.out.println(map.size());

System.out.println(map.isEmpty());

}

}

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!