Question: Java Programming NewTable Class methods.Could use some help with this one. Need to fill out methods listed below the main snippet. Thank you. import java.util.Iterator;

Java Programming NewTable Class methods.Could use some help with this one. Need to fill out methods listed below the main snippet. Thank you.

import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.Vector;

class HashPair{ K key; E element; }

public class NewTable{ private Vector< LinkedList< HashPair > > table; public E put (K key, E element){ if (key == null || element == null){ throw new NullPointerException("key or element is null!"); } //Set up a ListIterator that can step through the one linkned list that might //already have an element with the given key. int i = hash(key); LinkedList> onleList = table.get(i); ListIterator> cursor = oneList.listIterator(0); //Two other variables for the new HashPair(if needed) and the return value: HashPair pair; E answer; //Step Through the one linked list using the iterator while(cursor.hasNext()){ pair = cursor.next(); if (pair.key.equals(key)){ //we found the given key already on the list. answer = pair.element; pair.element = element; return element; } } //The specified key was not on oneList, so create a new node for the new entry: pair = new HashPair; pair.key = key; pair.element = element; oneList.add(pair); }

/** Implement methods these methods**/

//Constructor public NewTable(int capacity)

//The put method //precondition neither key nor element may be null. public E put(K key, E element)

//The containsKey method //precondition key cannot be null. true if this table contains an object with the specified key, false otherwise. public boolean containsKey(K key)

//The get method //precondition cannot be null. returns a reference to the object with the specified key. null otherwise. //key.equals() is used to compare the key to the keys that are in the table. public E get(K key)

//The remove method //precondition cannot be null //key.equals() is used to compare the key to the keys that are in the table. public E remove(K key)

//The clear method - Removes all entries in the hash table public void clear()

//The hash method - Returns the hash value of key public int hash(K key)

//The size method Returns the number of elements currently in the hash table public int size()

}

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!