Home Java Java-tips Data Collections Exercises Collections Exercise 4 - Word Translator

Ask Questions?

View Latest Questions


 
 

Collections Exercise 4 - Word Translator
Posted on: July 26, 2006 at 12:00 AM
The user will enter a word and the program will produce a list of "translations".

Java: Collections Exercise 4 - Word Translator

333. leftnewads2.shtml" -->

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 iterator(String word), which returns an iterator over all definitions for the given word. It returns null if there are no definitions.

Solution

    Map m_dictionary = new HashMap();
    
    /** Adds a new definition for a word. 
     *  If this is the first time the word is being defined, 
     *     it is entered into a map as the key, and an ArrayList is entered
     *     as the value.
     *  If the word already has a definition, it adds this new definition
     *     to the existing ArrayList of definitions.
     *@param word Word in the dictionary.
     *@param definition A definition for word.
     */
    public void AddWord(String word, String definition) {
        //... Get the existing list of definitions for this word.
        ArrayList defs = (ArrayList)m_dictionary.get(word);
        
        //... If there wasn't already an entry for it, create one.
        if (defs == null) {
            defs = new ArrayList();
            m_dictionary.put(word, defs);
        }
        //... Add the target word to the list of target words.
        defs.add(definition);
    }
    
    /** Returns an iterator which will go over the collection of definitions
     *  for the given word.
     *@param word The word whose definitions are being requested.
     *@return An iterator over all defintions associated with word.
     *        If the word is not in the dictionary, null will be returned.
     */
    public Iterator iterator(String word) {
        ArrayList defs = (ArrayList)m_dictionary.get(word);
        if (defs == null) return null;
        
        return defs.iterator();
    }
Copyleft 2004 Fred Swartz MIT License

Related Tags for Collections Exercise 4 - Word Translator:
coopfileidearraylisttextdatahashdictionarydifftreeiomapiteratorstructuserwordlinklooptranslationarraylisthashmapstructuredefinitionreadlinethisidsetelementonlineexeoosimpleelementsshowiftexforexampleenglishprogramhashsettoinidictramreadingexamposcissishwordsexteilitstructuresliinitusetreemapimmanceenterinrmpassdifferentgermanasmnttrsidsideososlinkedadesemoverrcmehowprotortreesxawhichxampuatsxermaneeginatosskisinkhallivmplrayfolloweadudefinitionsandarconsstrrdssimxttreesetwingslassrdrenthshoavbeginstatiaphatfefinosespleplprodprmindonlyonogrolonl