Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulated a translation dictionary. Your program must be ab

ID: 3671864 • Letter: W

Question

 Write a program that simulated a translation dictionary. Your program must be able to support any number of words and multiple languages. Input must be read in from a text file, where each line is in the following format: <word>:<language>:<word in english>  examples: pollo:spanish:chicken hund:german:dog perro:spanish:dog you need to support translation to and from multiple languages and English. For an English word "dog" and a language "Spanish" you'd need to be able to produce the answer "perro". Likewise, for a foreign word "perro" and a language "Spanish" you'd need to be able to produce the English word "dog". Below are three methods to get started.  you do need to show that you're able to read your input from a file. I recommend using aScanner object wrapped around a File object.   public class TranslationDictionary {    public TranslationDictionary(String inputFileName) {     // load up the dictionary from the file here   }    public String englishToForeign(String foreignLanguage, String englishWord) {     // do something here   }    public String foreignToEnglish(String foreignLanguage, String foreignWord) {     // do something here   } } 

Explanation / Answer

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class TranslationDictionary {

   List<Word> words = new ArrayList<Word>();

   public TranslationDictionary(String inputFileName) {
       // load up the dictionary from the file here
       Scanner scanner = null;
       try {
           scanner = new Scanner(new File(inputFileName));

           while (scanner.hasNext()) {
               String line = scanner.nextLine();

               String lineArr[] = line.split(":");

               words.add(new Word(lineArr[0], lineArr[1], lineArr[2]));

           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }

   public String englishToForeign(String foreignLanguage, String englishWord) {
       // do something here
       String foreignWord = "";
       Iterator<Word> wordIT = words.iterator();
       while (wordIT.hasNext()) {
           Word word = wordIT.next();
           if (word.getWordInEnglish().equals(englishWord)
                   && word.getLanguage().equals(foreignLanguage))
               foreignWord = word.getWord();

       }

       return foreignWord;
   }

   public String foreignToEnglish(String foreignLanguage, String foreignWord) {
       // do something here
       String englishWord = "";
       Iterator<Word> wordIT = words.iterator();
       while (wordIT.hasNext()) {
           Word word = wordIT.next();
           if (word.getWord().equals(foreignWord)
                   && word.getLanguage().equals(foreignLanguage))
               englishWord = word.getWordInEnglish();

       }

       return englishWord;
   }

   public static void main(String[] args) {
       TranslationDictionary dictionary = new TranslationDictionary(
               "inputFile.txt");
       System.out.println("englishToForeign=="
               + dictionary.englishToForeign("german", "dog"));
       System.out.println("englishToForeign=="
               + dictionary.foreignToEnglish("german", "hund"));

   }
}

// <word>:<language>:<word in english>
class Word {
   String word, language, wordInEnglish;

   /**
   * @param word
   * @param language
   * @param wordInEnglish
   */
   public Word(String word, String language, String wordInEnglish) {

       this.word = word;
       this.language = language;
       this.wordInEnglish = wordInEnglish;
   }

   /**
   * @return the word
   */
   public String getWord() {
       return word;
   }

   /**
   * @return the language
   */
   public String getLanguage() {
       return language;
   }

   /**
   * @return the wordInEnglish
   */
   public String getWordInEnglish() {
       return wordInEnglish;
   }

   /**
   * @param word
   * the word to set
   */
   public void setWord(String word) {
       this.word = word;
   }

   /**
   * @param language
   * the language to set
   */
   public void setLanguage(String language) {
       this.language = language;
   }

   /**
   * @param wordInEnglish
   * the wordInEnglish to set
   */
   public void setWordInEnglish(String wordInEnglish) {
       this.wordInEnglish = wordInEnglish;
   }

}

OUTPUT:

englishToForeign==hund
englishToForeign==dog

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote