package lab09; import java.util.*; /* Lab description: * * We give below "non-generic" versions of Entry, WordListNode, WordListIterator * and WordList classes. By non-generic we mean that the Entry class is programmed * to store an Integer keyValue and a String dataValue. * * We wish to allow the Entry class to store arbitrary datatypes for dataKey and dataValue. * Note that dataKey and dataValue can have the same type (e.g., both can be Strings) * or can be of different types (Integer and String in the above example). We hence * create a "generic" Entry class with two parameterized types (K & V) that looks * as follows, * * class Entry { * K dataKey; // dataKey is determined by input type K * V dataValue; // dataValue is determined by V which can be different from K * * // .... methods in Entry class * } * * Examples: * Entry corresponds to an Entry class * with Integer key and String data * * Entry corresponds to an Entry class * with String key and String data * * TASKS: * (a) Follow the generic Entry class example above and modify it and other classes * to create their corresponding generic versions. * Note that some generic class instances are already present * (e.g., Iterator, Iterable). Be careful when you modify these. * * (b) Change HashClient.java to use the generic classes that you have created. */ // The classes below are non-generic, change to their generic implementation class Entry { K dataKey; V dataValue; Entry(K newKey, V newValue) {dataKey = newKey; dataValue = newValue;} K getKey(){ return dataKey; } V getValue(){ return dataValue; } } class WordListNode { Entry nodeEntry; WordListNode nextNode; WordListNode(Entry newEntry, WordListNode newNext){ nodeEntry = newEntry; nextNode = newNext; } Entry getEntry(){ return nodeEntry; } WordListNode getNext(){ return nextNode; } } class WordListIterator implements Iterator> { WordListNode currentWordListNode; WordListIterator( WordListNode wordListHead ) { currentWordListNode = wordListHead; } public boolean hasNext() { return (currentWordListNode != null); } public Entry next() { Entry currentEntry = currentWordListNode.getEntry(); currentWordListNode = currentWordListNode.getNext(); return currentEntry; } public void remove(){} } public class WordList implements Iterable> { WordListNode wordListHead; WordList() { wordListHead = null; } void addToHead( Entry newEntry ) { wordListHead = new WordListNode( newEntry, wordListHead ); } public Iterator> iterator() { return new WordListIterator( wordListHead ); } }