Home Java Java-tips Data Collections Exercises Collections Exercise 4 - Word Translator (Generics)

Ask Questions?

View Latest Questions


 
 

Collections Exercise 4 - Word Translator (Generics)
Posted on: July 26, 2006 at 12:00 AM
For passing over the elements of a data structure, consider only Iterator and for loop.

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

  1. Declare and initialize a variable, m_dictionary, which will hold each source word (German in the example above) and the collection of translations (English).
  2. 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).
  3. 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.

Answer

Copyleft 2004 Fred Swartz MIT License

Related Tags for Collections Exercise 4 - Word Translator (Generics):
cdesignstringfilelisttextdatadictionarystringsdiffiostructmultipleuserwordiptranslationstructuredefinitionreadlineonlineoosimplelikeshowiftexforexampleuncenglishprogramtoinidictramreadingfastexamstoreposastssiwsshwordsexteilsignitdesmultinotliinituseulimmanceenterinnormdifferentgermanasmnttrososadesallmehowprotorxawhichxampuatsrmantipssasoeeginessatosskishallivnotemplfolloweadudefinitionsandarccstrrdssasimxttzwingzslassriringrdrenthshoavbeginstaccessabatihatfeesifinesiiplpleplprodprmindonomogrolonl