Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions

Question

Experience in a leadership role in a team

Answered: 1 week ago

Question

Which personal relationships influenced you the most?

Answered: 1 week ago

Question

What were your most important educational experiences?

Answered: 1 week ago

Question

How was your life influenced by those events?

Answered: 1 week ago