Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Collections Exercise 4 - Word Translator

[an error occurred while processing this directive]

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();
    }

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.