Java: Collections Exercise 4 - Word Translator (Generics)
Name ____________________________________
For the purposes of this exercise, the only data structures that need to be considered are: array, ArrayList, LinkedList, HashSet, TreeSet, HashMap, and TreeMap. For passing over the elements of a data structure, consider only Iterator and for loop.
Situation: An online dictionary is needed. The user will enter a word and the program will produce a list of "translations". The program begins by reading a file of definitions, which give the different possible translations of a word. For example, a simple text file might have the following structure for showing the English translation of German words.
absatteln=unsattle Absatz=heel Absatz=paragraph Absatz=ledge Absatz=sales |
Note that some words, like "Absatz" (I had to look it up recently), have several translations. It's necessary to store multiple translations for a given word. Design a data structure that allows fast access, given a word (String), to its translations (a bunch of Strings). |
Problems
- Declare and initialize a variable, m_dictionary, which will hold each source word (German in the example above) and the collection of translations (English).
- Write a method,
public void AddWord(String word, String definition), which associates the definition with the word. It will add definition to the collection of existing definitions for this word (or create the first entry if there isn't one). -
Write a method,
public Iterator<String> iterator(String word), which returns an iterator over all definitions for the given word. It returns null if there are no definitions.















