[Solved]Java Data Structures File Unsortedlinkeddictionaryjava Contains Unimplemented Methods Dict Q37120992
Java Data structures.
The file UnsortedLinkedDictionary.java containsunimplemented methods for a dictionary based on an unsorted linkedlist. Implement all these methods.
On the other word, modifyUnsortedArrayDictionary.java andUnsortedArrayDictionaryDriver.java from Array toLinkList
———————————————————————————————————————–
UnsortedArrayDictionary.java :
import java.util.Arrays;import java.util.Iterator;import java.util.NoSuchElementException;public class UnsortedArrayDictionary<K, V>{ private Entry<K, V>[] dictionary; // array of unsorted entries private int numberOfEntries; private final static int DEFAULT_INITIAL_CAPACITY = 6; public UnsortedArrayDictionary() { this(DEFAULT_INITIAL_CAPACITY); } // end default constructor public UnsortedArrayDictionary(int initialCapacity) { // the cast is safe because the new array contains null entries @SuppressWarnings(“unchecked”) Entry<K, V>[] tempDictionary = (Entry<K, V>[])new Entry[initialCapacity]; dictionary = tempDictionary; numberOfEntries = 0; } // end constructor public V add(K key, V value) { V result = null; int keyIndex = locateIndex(key); if (keyIndex < numberOfEntries) { // key found, return and replace old value result = dictionary[keyIndex].getValue(); // old value dictionary[keyIndex].setValue(value); // replace value } else { ensureCapacity(); // add at end of array dictionary[numberOfEntries] = new Entry<K, V>(key, value); numberOfEntries++; } // end if return result; } // end add public V remove(K key) { V result = null; int keyIndex = locateIndex(key); if (keyIndex < numberOfEntries) { result = dictionary[keyIndex].getValue(); dictionary[keyIndex] = dictionary[numberOfEntries – 1]; // replace removed entry with last entry numberOfEntries–; } // end if // else result is null return result; } // end remove public V getValue(K key) { V result = null; int keyIndex = locateIndex(key); if (keyIndex < numberOfEntries) { result = dictionary[keyIndex].getValue(); } // end if return result; } // end getValue public boolean contains(K key) { return getValue(key) != null; } // end contains public boolean isEmpty() { return numberOfEntries == 0; } // end isEmpty public int getSize() { return numberOfEntries; } // end getSize public final void clear() { numberOfEntries = 0; } // end clear public Iterator<K> getKeyIterator() { return new KeyIterator(); } // end getKeyIterator public Iterator<V> getValueIterator() { return new ValueIterator(); } // end getValueIterator // Returns the index of the entry that contains the given search key, or // numberOfEntries if no such entry exists. private int locateIndex(K key) { // sequential search int index = 0; while ( (index < numberOfEntries) && !key.equals(dictionary[index].getKey()) ) index++; return index; } // end locateIndex // Doubles the size of the array dictionary if it is full private void ensureCapacity() { if (numberOfEntries == dictionary.length) dictionary = Arrays.copyOf(dictionary, 2 * dictionary.length); } // end ensureCapacity // Same as SortedArrayDictionary private class KeyIterator implements Iterator<K> { private int currentIndex; private KeyIterator() { currentIndex = 0; } // end default constructor public boolean hasNext() { return currentIndex < numberOfEntries; } // end hasNext public K next() { K result = null; if (hasNext()) { Entry<K,V> currentEntry = dictionary[currentIndex]; result = currentEntry.getKey(); currentIndex++; } else throw new NoSuchElementException(); return result; } // end next public void remove(){ throw new UnsupportedOperationException(); } // end remove } // end KeyIterator private class ValueIterator implements Iterator<V> { private int currentIndex; private ValueIterator() { currentIndex = 0; } // end default constructor public boolean hasNext() { return currentIndex < numberOfEntries; } // end hasNext public V next() { V result = null; if (hasNext()) { Entry<K,V> currentEntry = dictionary[currentIndex]; result = currentEntry.getValue(); currentIndex++; } else throw new NoSuchElementException(); return result; } // end next public void remove() { throw new UnsupportedOperationException(); } // end remove } // end getValueIterator private class Entry<S, T> { private S key; private T value; private Entry(S searchKey, T dataValue) { key = searchKey; value = dataValue; } // end constructor private S getKey() { return key; } // end getKey private T getValue() { return value; } // end getValue private void setValue(T dataValue) { value = dataValue; } // end setValue } // end Entry} // end UnsortedArrayDictionary
—————————————————————————————
UnsortedArrayDictionaryDriver.java
import java.util.Iterator;public class UnsortedArrayDictionaryDriver { public static void main(String[] args) { testDictionary(); } // end main public static void display(UnsortedArrayDictionary<String, String> dictionary) { Iterator<String> keyIterator = dictionary.getKeyIterator(); Iterator<String> valueIterator = dictionary.getValueIterator(); while (keyIterator.hasNext() && valueIterator.hasNext()) System.out.println(keyIterator.next() + ” : ” + valueIterator.next()); System.out.println(); } // end display public static void testDictionary() { String dirk = “Dirk”; String abel = “Abel”; String miguel = “Miguel”; String tabbie = “Tabatha”; String tom = “Tom”; String sam = “Sam”; String reiss = “Reiss”; String bette = “Bette”; String carole = “Carole”; String derek = “Derek”; String nancy = “Nancy”; String bogus = “Bo”; // create a dictionary System.out.println(“Creating dictionary:n”); UnsortedArrayDictionary<String, String> nameList = new UnsortedArrayDictionary<String, String>(); System.out.println(“nnTesting isEmpty():n”); if(nameList.isEmpty() == true) System.out.println(“OK”); else System.out.println(“ERROR”); // test add System.out.println(“nnTesting add():n”); nameList.add(dirk, “555-1234”); nameList.add(abel, “555-5678”); nameList.add(miguel, “555-9012”); nameList.add(tabbie, “555-3456”); nameList.add(tom, “555-5555”); nameList.add(sam, “555-7890”); nameList.add(reiss, “555-2345”); nameList.add(bette, “555-7891”); nameList.add(carole, “555-7892”); nameList.add(derek, “555-7893”); nameList.add(nancy, “555-7894”); System.out.println(“nnTesting getSize():n”); if(nameList.getSize() == 11) System.out.println(“OK”); else System.out.println(“ERROR”); display(nameList); // test getValue System.out.println(“nnTesting getValue():n”); System.out.println(“nSam: ” + nameList.getValue(sam) + ” should be 555-7890″); System.out.println(“nTom: ” + nameList.getValue(tom) + ” should be 555-5555″); System.out.println(“nReiss: ” + nameList.getValue(reiss) + ” should be 555-2345″); // test contains System.out.println(“nnnTesting contains():n”); if ( nameList.contains(sam) ) System.out.println(“nSam is in dictionary – OK”); else System.out.println(“Error in contains()”); if ( nameList.contains(abel) ) System.out.println(“nAbel is in dictionary – OK”); else System.out.println(“Error in contains()”); if ( nameList.contains(miguel) ) System.out.println(“nMiguel is in dictionary – OK”); else System.out.println(“Error in contains()”); if ( nameList.contains(tom) ) System.out.println(“nTom is in dictionary – OK”); else System.out.println(“Error in contains()”); if (!nameList.contains(bogus)) System.out.println(“nBo is not in dictionary – OK”); else System.out.println(“Error in contains()”); // remove first item System.out.println(“nnnRemoving first item Dirk – Dirk’s number is ” + nameList.remove(dirk) + ” should be 555-1234″); // test replacing value System.out.println(“Replacing phone number of Reiss and Miguel:n”); String oldNumber = nameList.add(reiss, “555-5432”); System.out.println(“Reiss’s old number ” + oldNumber + ” is replaced by 555-5432″); oldNumber = nameList.add(miguel, “555-2109”); System.out.println(“Miguel’s old number ” + oldNumber + ” is replaced by 555-2109″); System.out.println(“n” + nameList.getSize() + ” (should be 10) items in dictionary, as follows:n”); display(nameList); // remove interior and last items System.out.println(“nnRemoving interior item Reiss and last item Nancy:n”); nameList.remove(reiss); nameList.remove(nancy); // remove Bo (not in dictionary) System.out.println(“nRemoving Bo (not in dictionary):n”); String result = nameList.remove(bogus); if (result == null) System.out.println(“Bo was not found in the dictionary.”); else System.out.println(“Error in remove().”); System.out.println(“nn” + nameList.getSize() + ” (should be 8) items in dictionary, as follows:n”); display(nameList); // test clear System.out.println(“nnTesting clear():n”); nameList.clear(); System.out.println(“Dictionary should be empty; isEmpty() returns ” + nameList.isEmpty()); } // testDictionary } // end Driver
———————————————————————————————————————————
question start here:
UnsortedLinkedDictionary.java:
import java.util.Iterator;import java.util.NoSuchElementException;public class UnsortedLinkedDictionary<K, V> { public UnsortedLinkedDictionary() { } // end default constructor public V add(K key, V value) { } // end add public V remove(K key) { } // end remove public V getValue(K key) { } // end getValue public boolean contains(K key) { } // end contains public boolean isEmpty() { } // end isEmpty public int getSize() { } // end getSize public final void clear() { } // end clear public Iterator<K> getKeyIterator() { } // end getKeyIterator public Iterator<V> getValueIterator() { } // end getValueIterator private class Node { private K key; private V value; private Node next; private Node(K searchKey, V dataValue) { key = searchKey; value = dataValue; next = null; } // end constructor private Node(K searchKey, V dataValue, Node nextNode) { key = searchKey; value = dataValue; next = nextNode; } // end constructor private K getKey() { return key; } // end getKey private V getValue() { return value; } // end getValue private void setValue(V newValue) { value = newValue; } // end setValue private Node getNextNode() { return next; } // end getNextNode private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node} // end UnsortedLinkedDictionary
Expert Answer
Answer to Java Data structures. The file UnsortedLinkedDictionary.java contains unimplemented methods for a dictionary based on an… . . .
OR

