Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the below assignement, and am having trouble with the last 3 methods. I have done some of the code, but dont know how

I have the below assignement, and am having trouble with the last 3 methods. I have done some of the code, but dont know how to proceed. Please help.

The starter code contains HashNode.java, MyHashTable.java, and MyHashTableTester.java. Your job is to 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

//ArrayList list=new ArrayList();

ArrayList list;

// Current capacity of array list

int numbBuckets;

// Current size of array list

int size;

// Constructor (Initializes capacity, size and

// empty chains.

public MyHashTable(int cap, int size,ArrayList list)

{

numbBuckets= cap;

this.size = size;

this.list = list;

}

// Implement the size() method

public int size()

{

return list.size();

}

//Implement the isEmpty() method

public boolean isEmpty()

{

return list.isEmpty();

}

// 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

int indexKey = getBucketIndex(key);

int head;

}

// 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

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions