CS 535 Object-Oriented Programming & Design Spring Semester, 1999 Translator Example |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 11-Feb-99 |
Translator Assignment
Description
We are going to build a simple translator between languages. The translator will only translate single words. For example translate “cat” into Spanish (gato). Some words have different endings and forms depending on how they are used in a sentence. The past tense of “to have” is “had”. The translator will not handle checking for different forms of a word. The translations will be simple dictionary look-ups. Some words translate into more that one word. For example “house” can be translated into Spanish as “hogar” or “casa”, “homeless” translates into “sin hogar”. The translator will contain a database or dictionary of word translations for each language pair. Initially the structure of the database can be a sorted list of word pairs. For efficiency reason this might have to change in the future. The translator will start with the language pairs English-Spanish, and English-German. A user can ask for the translation of word in one language to another. Given the two language pairs above, a user has the option of translating a word from English to either Spanish or German, Spanish to either English or German, and German to either English of Spanish. A user can also add new words to the database of a language pair. A use can add words at a time, or by providing a file of words. A user can add additional translations for a given word, or modify an existing translation for a word (errors to occur and words can change meaning). The initial user interface to the translator will be ASCII based, but may change to a GUI later. Another possible future feature is the ability to link the translator to other program. For example, one may use the translator as a back end to a word processor.
Strawman Solutions
Two partial implementations for the translator are given. These will be used to talk about coupling and cohesion issues. The implementations are not meant to be examples of good style. In fact, the goal of the strawman solutions was to do many things poorly. The question is what is done poorly and how to correct them.
Strawman Solution 1
import java.util.HashMap; import sdsu.io.Console; public class Translator { private HashMap englishToSpanish; private HashMap spanishToEnglish; private HashMap englishToGerman; private HashMap germanToEnglish; public Translator() { //for testing only, replace with file access later englishToSpanish = new HashMap(); spanishToEnglish = new HashMap(); englishToGerman = new HashMap(); germanToEnglish = new HashMap(); englishToSpanish.put( "athlete", "atleta" ); englishToSpanish.put( "budget", "presupuesto" ); englishToSpanish.put( "mother", "madre" ); spanishToEnglish.put( "atleta", "athlete" ); spanishToEnglish.put( "presupuesto", "budget" ); spanishToEnglish.put( "madre", "mother" ); englishToGerman.put( "athlete", "athletik" ); englishToGerman.put( "budget", "vorrat" ); englishToGerman.put( "mother", "mutter" ); germanToEnglish.put( "athletik", "athlete" ); germanToEnglish.put( "vorrat", "budget" ); germanToEnglish.put( "mutter", "mother" ); } public String englishToGerman( String englishWord ) { Object german = englishToGerman.get( englishWord ); if (german == null ) return "***" + englishWord + " Not Found***"; else return (String) german; } public String englishToSpanish( String englishWord ) { Object spanish = englishToSpanish.get( englishWord ); if (spanish == null ) return "***" + englishWord + " Not Found***"; else return (String) spanish; } public String germanToEnglish( String germanWord ) { Object english = germanToEnglish.get( germanWord ); if (english == null ) return "***" + germanWord + " Not Found***"; else return (String) english; } public String spanishToEnglish( String spanishWord ) { Object english = spanishToEnglish.get( spanishWord ); if (english == null ) return "***" + spanishWord + " Not Found***"; else return (String) english; } public String germanToSpanish( String germanWord ) { return englishToSpanish(germanToEnglish( germanWord )); } public String spanishToGerman( String spanishWord ) { return englishToGerman(spanishToEnglish( spanishWord )); } public void run() { while (true) { int selection = -1; while ( (selection < 1) || (selection > 5 )) { Console.println( "Select on of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. Add new Language" ); Console.println( " 2. Add new words" ); Console.println( " 3. Modify existing words" ); Console.println( " 4. Translate a word" ); Console.println( " 5. Quit" ); selection = Console.readInt( "Type a integer 1 - 5 to select your option: " ); Console.flushLine(); Console.println(); } switch( selection ) { case 1: case 3: Console.println( "Sorry, this feature not available" ); Console.println(); break; case 2: int language = -1; while ( (language < 1) || (language > 4 )) { Console.println( "Select on of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. English to Spanish" ); Console.println( " 2. English to German" ); Console.println( " 3. German to English" ); Console.println( " 4. Spanish to English" ); language = Console.readInt( "Type a integer 1 - 4 to select your option: " ); Console.flushLine(); Console.println(); } String wordOne = Console.readWord( "Type the first word: " ); Console.flushLine(); String wordTwo = Console.readWord( "Type the second word: " ); Console.flushLine(); switch ( language ) { case 1: englishToSpanish.put( wordOne, wordTwo ); spanishToEnglish.put( wordTwo, wordOne ); break; case 2: englishToGerman.put( wordOne, wordTwo ); germanToEnglish.put( wordTwo, wordOne ); break; case 3: germanToEnglish.put( wordOne, wordTwo ); englishToGerman.put( wordTwo, wordOne ); break; case 4: spanishToEnglish.put( wordOne, wordTwo ); englishToSpanish.put( wordTwo, wordOne ); break; } Console.println(); break; case 4: language = -1; while ( (language < 1) || (language > 6 )) { Console.println( "Select on of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. English to Spanish" ); Console.println( " 2. English to German" ); Console.println( " 3. German to English" ); Console.println( " 4. German to Spanish" ); Console.println( " 5. Spanish to English" ); Console.println( " 6. Spanish to German" ); language = Console.readInt( "Type a integer 1 - 6 to select your option: " ); Console.flushLine(); Console.println(); } String word = Console.readWord( "Type the word to translate: " ); Console.flushLine(); switch ( language ) { case 1: String translation = englishToSpanish( word ); Console.println( word + " in Spanish is " + translation ); break; case 2: translation = englishToGerman( word ); Console.println( word + " in German is " + translation ); break; case 3: translation = germanToEnglish( word ); Console.println( word + " in English is " + translation ); break; case 4: translation = germanToSpanish( word ); Console.println( word + " in Spanish is " + translation ); break; case 5: translation = spanishToEnglish( word ); Console.println( word + " in English is " + translation ); break; case 6: translation = spanishToGerman( word ); Console.println( word + " in German is " + translation ); break; } Console.println(); break; case 5: Console.println( "Goodbye" ); return; } } } }
Strawman Solution 2
import java.util.HashMap; import sdsu.io.Console; public class Translator { private static final int ADD_LANUAGE = 1; private static final int ADD_WORDS = 2; private static final int MODIFY_WORDS = 3; private static final int TRANSLATE = 4; private static final int QUIT = 5; private static final int ENGLISH_SPANISH = 1; private static final int ENGLISH_GERMAN = 2; private static final int GERMAN_ENGLISH = 3; private static final int SPANISH_ENGLISH = 4; private HashMap englishToSpanish; private HashMap spanishToEnglish; private HashMap englishToGerman; private HashMap germanToEnglish; public Translator() { //for testing only, replace with file access later englishToSpanish = new HashMap(); spanishToEnglish = new HashMap(); englishToGerman = new HashMap(); germanToEnglish = new HashMap(); setEnglishSpanish( "athlete", "atleta" ); setEnglishSpanish( "budget", "presupuesto" ); setEnglishSpanish( "mother", "madre" ); setEnglishGerman( "athlete", "athletik" ); setEnglishGerman( "budget", "vorrat" ); setEnglishGerman( "mother", "mutter" ); } public void setEnglishSpanish( String englishWord, String spanishWord ) { englishToSpanish.put( englishWord, spanishWord ); spanishToEnglish.put( spanishWord, englishWord ); } public void setEnglishGerman( String englishWord, String germanWord ) { englishToGerman.put( englishWord, germanWord ); germanToEnglish.put( germanWord, englishWord ); } public String englishToGerman( String englishWord ) { Object german = englishToGerman.get( englishWord ); if (german == null ) return "***" + englishWord + " Not Found***"; else return (String) german; } public String englishToSpanish( String englishWord ) { Object spanish = englishToSpanish.get( englishWord ); if (spanish == null ) return "***" + englishWord + " Not Found***"; else return (String) spanish; } public String germanToEnglish( String germanWord ) { Object english = germanToEnglish.get( germanWord ); if (english == null ) return "***" + germanWord + " Not Found***"; else return (String) english; } public String spanishToEnglish( String spanishWord ) { Object english = spanishToEnglish.get( spanishWord ); if (english == null ) return "***" + spanishWord + " Not Found***"; else return (String) english; } public String germanToSpanish( String germanWord ) { return englishToSpanish(germanToEnglish( germanWord )); } public String spanishToGerman( String spanishWord ) { return englishToGerman(spanishToEnglish( spanishWord )); } public void run() { Console.println( "Start" ); while (true) { int selection = selectFeature(); switch( selection ) { case ADD_LANUAGE: addLanguage(); break; case MODIFY_WORDS: modifyWords(); break; case ADD_WORDS: addWords(); break; case TRANSLATE: translate(); break; case QUIT: Console.println( "Goodbye" ); return; } } } private int selectFeature() { int selection = -1; while ( (selection < 1) || (selection > 5 )) { Console.println( "Select on of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. Add new Language" ); Console.println( " 2. Add new words" ); Console.println( " 3. Modify existing words" ); Console.println( " 4. Translate a word" ); Console.println( " 5. Quit" ); selection = Console.readInt( "Type a integer 1 - 5 to select your option: " ); Console.flushLine(); Console.println(); } return selection; } private void addLanguage() { Console.println( "Sorry, this feature not available" ); Console.println(); } private void modifyWords() { Console.println( "Sorry, this feature not available" ); Console.println(); } private void addWords() { int languagePair = selectLanguagePair(); String wordOne = Console.readWord( "Type the first word: " ); Console.flushLine(); String wordTwo = Console.readWord( "Type the second word: " ); Console.flushLine(); switch ( languagePair ) { case ENGLISH_SPANISH: setEnglishSpanish( wordOne, wordTwo ); break; case ENGLISH_GERMAN: setEnglishGerman( wordOne, wordTwo ); break; case GERMAN_ENGLISH: germanToEnglish.put( wordOne, wordTwo ); break; case SPANISH_ENGLISH: spanishToEnglish.put( wordOne, wordTwo ); break; } Console.println(); } private int selectLanguagePair() { int language = -1; while ( (language < 1) || (language > 4 )) { Console.println( "Select one of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. English to Spanish" ); Console.println( " 2. English to German" ); Console.println( " 3. German to English" ); Console.println( " 4. Spanish to English" ); language = Console.readInt( "Type a integer 1 - 4 to select your option: " ); Console.flushLine(); Console.println(); } return language; } private void translate() { int language = -1; while ( (language < 1) || (language > 6 )) { Console.println( "Select one of the following options" ); Console.println( "-----------------------------------------------" ); Console.println( " 1. English to Spanish" ); Console.println( " 2. English to German" ); Console.println( " 3. German to English" ); Console.println( " 4. German to Spanish" ); Console.println( " 5. Spanish to English" ); Console.println( " 6. Spanish to German" ); language = Console.readInt( "Type a integer 1 - 6 to select your option: " ); Console.flushLine(); Console.println(); } String word = Console.readWord( "Type the word to translate: " ); Console.flushLine(); switch ( language ) { case 1: String translation = englishToSpanish( word ); Console.println( word + " in Spanish is " + translation ); break; case 2: translation = englishToGerman( word ); Console.println( word + " in German is " + translation ); break; case 3: translation = germanToEnglish( word ); Console.println( word + " in English is " + translation ); break; case 4: translation = germanToSpanish( word ); Console.println( word + " in Spanish is " + translation ); break; case 5: translation = spanishToEnglish( word ); Console.println( word + " in English is " + translation ); break; case 6: translation = spanishToGerman( word ); Console.println( word + " in German is " + translation ); break; } Console.println(); } }
Copyright © 1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.
Previous    visitors since 11-Feb-99    Next