Question: Please help with this in JAVA language please. Thanks Create a class HashedDictionary. A class that implements the ADT Hashing by using a chain of

Please help with this in JAVA language please. Thanks

Create a class HashedDictionary. A class that implements the ADT Hashing by using a chain of TableEntry.

Please help with this in JAVA language please. Thanks Create a classHashedDictionary. A class that implements the ADT Hashing by using a chain

of TableEntry. import java.util.Iterator; import java.util.NoSuchElementException; public class HashedDictionary implements Dictionarylnterface //

The dictionary: private int numberOfEntries; private static final int DEFAULT_CAPACITY =5;// Mustbe prime private static final int MAXCAPACITY =10000; // The hash table:private TableEntry[] hashTable; private int tablesize; // Must be prime private static

final int MAXSIZE=2MAX_CAPACITY; private boolean initialized = false; private static final doubleMAX_LOAD_FACTOR =0.5;// Fraction of hash table that can be filled public HashedDictionary()

this(DEFAULT_CAPACITY); // Call next constructor ]// end default constructor public HashedDictionary(int initialCapacity)checkCapacity(initialCapacity); numberOfEntries =0;// Dictionary is empty // Set up hash table: //

Initial size of hash table is same as initialCapacity if it is

prime; // otherwise increase it until it is prime size int tableSize

= getNextPrime(initialCapacity); checkSize(tableSize); // The cast is safe because the new arraycontains null entries @SuppressWarnings("unchecked") TableEntry[] temp =( TableEntry []) new TableEntry[tableSize]; hashTable= temp; initialized = true; \} // end constructor // // We've

import java.util.Iterator; import java.util.NoSuchElementException; public class HashedDictionary implements Dictionarylnterface // The dictionary: private int numberOfEntries; private static final int DEFAULT_CAPACITY =5;// Must be prime private static final int MAXCAPACITY =10000; // The hash table: private TableEntry[] hashTable; private int tablesize; // Must be prime private static final int MAXSIZE=2MAX_CAPACITY; private boolean initialized = false; private static final double MAX_LOAD_FACTOR =0.5;// Fraction of hash table that can be filled public HashedDictionary() this(DEFAULT_CAPACITY); // Call next constructor ]// end default constructor public HashedDictionary(int initialCapacity) checkCapacity(initialCapacity); numberOfEntries =0;// Dictionary is empty // Set up hash table: // Initial size of hash table is same as initialCapacity if it is prime; // otherwise increase it until it is prime size int tableSize = getNextPrime(initialCapacity); checkSize(tableSize); // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") TableEntry[] temp =( TableEntry []) new TableEntry[tableSize]; hashTable = temp; initialized = true; \} // end constructor // // We've added this method to display the hash table for illustration and testing // public void displayHashTable0 checklnitialization(); for (int index =0; index = null) System.out.println("null "); else if (hashTable[index].isRemoved()) System.out.println("removed state"); else System.out.println(hashTable[index].getKey() + " " + hashTable[index].getValue()); ] // end for System.out.println(); ] // end displayHashTable // public V add( K key, V value) //You need to write "add" method \} // end add public V remove(K key) //You need to write "remove" method \} // end remove public V getValue(K key) //You need to write "getValue" method \}// end getValue public boolean contains( K key) // You need to write "contains" method ] // end contains public boolean isEmpty() //You need to write "isEmpty" method ] // end isEmpty public int getSize0 \{ //You need to write "getSize" method \}// end getSize public final void clear() public final void clear0 // You need to write "clear" method ] // end clear public Iterator getKeylterator() //You need to write "getKeylterator" method ]// end getKeylterator public Iterator getValuelterator0 //You need to write "getValuelterator" method ]// end getValuelterator private int getHashindex(K key) int hashindex = key.hashCode() \% hashTable.length; if (hashindex w twice its old size. // In doing so, this method must rehash the table entries. // In doing so, this method must rehash the table entries. // Precondition: checklnitialization has been called. private void enlargeHashTable() TableEntry [] oldTable = hashTable; int oldSize = hashTable.length; int newSize = getNextPrime(oldSize + oldSize); checkSize(newSize); // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") TableEntry[] tempTable = (TableEntry[])new TableEntry[newSize]; // Increase size of array hashTable = tempTable; numberOfEntries =0;// Reset number of dictionary entries, since // it will be incremented by add during rehash // Rehash dictionary entries from old array to the new and bigger array; // skip both null locations and removed entries for (int index =0; index && old Table[index].isln()) add(oldTable[index].getKey(), oldTable[index].getValue()); ] // end for ]// end enlargeHashTable // Returns true if lambda > MAX_LOAD_FACTOR for hash table; // otherwise returns false. private boolean is HashTableTooFull() private boolean is HashTableTooFull() return numberOfEntries > MAX_LOAD_FACTOR * hashTable.length; 3// end isHashTableTooFull // Returns a prime integer that is > w the given integer. private int getNextPrime(int integer) \{ // if even, add 1 to make odd if (integer \% 2=0 ) \{ integer++; ]// end if // test odd integers while ('isPrime(integer)) integer = integer +2; ] // end while return integer; \}// end getNextPrime // Returns true if the given integer is prime. private boolean isPrime(int integer) \{ boolean result; boolean done = false; //1 and even numbers are not prime if ( (integer =1)( integer %2=0) ) if ( (integer =1)( integer %2=0) ) result = false; ] //2 and 3 are prime else if ( (integer m=2)( integer m=3) ) \{ result = true; \} else // integer is odd and >=5 \{ assert (integer %2!=0)&&( integer >=5 ); // a prime is odd and not divisible by every odd integer up to its square root result = true; // assume prime for (int divisor =3; !done && (divisor divisor MAX_CAPACITY) throw new IllegalStateException("Attempt to create a dictionary " + "whose capacity is larger than " + MAX_CAPACITY); ]// end checkCapacity // Throws an exception if the hash table becomes too large. private void checkSize(int size) \{ if (tableSize > MAX_SIZE) throw new IllegalStateException("Dictionary has become too large."); ]// end checkSize private class Keylterator implements Iterator \{ private int currentindex; // Current position in hash table private int currentindex; // Current position in hash table private int numberLeft; // Number of entries left in iteration private Keylterator() currentlndex =0; numberLeft = numberOfEntries; ] // end default constructor public boolean hasNext() return numberLeft > 0 ; ] // end hasNext public K next() K result = null; if (has Next()) \{ // Skip table locations that do not contain a current entry while ( (hashTable[currentindex] = = null) || hashTable[currentindex].isRemoved() ) currentlndex++; \} // end while result = hashTable[currentindex].getKey(); numberLeft--; currentlndex++; \} else else throw new NoSuchElementException(); return result; ] // end next public void remove() throw new UnsupportedOperationException(); ] // end remove \} // end Keylterator private class Valuelterator implements Iterator ? \{ private int current Index; private int numberLeft; private Valuelterator() currentlndex = 0; numberLeft = numberOfEntries; \}// end default constructor public boolean hasNext() \{ return numberLeft >0; ] // end hasNext public V next() \{ V result = null; if (hasNext()) if (hasNext()) // Skip table locations that do not contain a current entry while ( (hashTable[currentIndex] = w null) || hashTable[currentindex].isRemoved()) currentlndex++; ] // end while result = hashTable[currentIndex].getValue(); numberLeft--; currentindex++; \} else throw new NoSuchElementException(); return result; \}// end next public void remove? throw new UnsupportedOperationException(); ] // end remove \} // end Valuelterator private static class TableEntry S,T> \{ private S key; private T value; private States state; // Flags whether this entry is in the hash table private enum States \{CURRENT, REMOVED }// Possible values of state private enum States \{CURRENT, REMOVED }// Possible values of state private TableEntry(S searchKey, T dataValue) key=searchKey;value=dataValue;state=States.CURRENT; \}// end constructor private S getKey0 return key; ] // end getkey private T getValue() return value; \} // end getValue private void setValue(T newValue) value = newValue; \} // end setValue // Returns true if this entry is currently in the hash table. private boolean isln() return state = intates.CURRENT J // end isln // Returns true if this entry has been removed from the hash table. private boolean isRemoved() private boolean isRemoved() return state = States.REMOVED; ] /// end isRemoved // Sets the state of this entry to be removed. private void setToRemoved() key = null; value = null; state = States.REMOVED; // Entry not in use, ie deleted from table ] /// end setToRemoved // Sets the state of this entry to current. private void setToln() // Not used state = States.CURRENT; // Entry in use ] // end setToln ] /// end TableEntry \} // end HashedDictionary

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!