// TranslateAlternative.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-11-19 File created. // 2005-11-19 Last modification. // This program works like Translate.java, but this one // does not use a command line parameter. import java.util.* ; class BilingualTranslation { protected String first_word ; protected String second_word ; public BilingualTranslation() {} public BilingualTranslation( String given_first_word, String given_second_word ) { first_word = given_first_word ; second_word = given_second_word ; } public boolean translate( String given_word ) { boolean translation_was_successful = false ; if ( given_word.equals( first_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + second_word + "\"" ) ; translation_was_successful = true ; } if ( given_word.equals( second_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\"" ) ; translation_was_successful = true ; } return translation_was_successful ; } } class TrilingualTranslation extends BilingualTranslation { protected String third_word ; public TrilingualTranslation( String given_first_word, String given_second_word, String given_third_word ) { first_word = given_first_word ; second_word = given_second_word ; third_word = given_third_word ; } public boolean translate( String given_word ) { boolean translation_was_successful = false ; if ( given_word.equals( first_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + second_word + "\" and \"" + third_word + "\"" ) ; translation_was_successful = true ; } if ( given_word.equals( second_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\" and \"" + third_word + "\"" ) ; translation_was_successful = true ; } if ( given_word.equals( third_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\" and \"" + second_word + "\"" ) ; translation_was_successful = true ; } return translation_was_successful ; } } class TranslateAlternative { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; ArrayList array_of_translations = new ArrayList() ; array_of_translations.add( new BilingualTranslation( "week", "semana" ) ) ; array_of_translations.add( new TrilingualTranslation( "street", "calle", "rue" ) ) ; array_of_translations.add( new BilingualTranslation( "eat", "comer" ) ) ; array_of_translations.add( new TrilingualTranslation( "woman", "mujer", "femme" ) ) ; array_of_translations.add( new TrilingualTranslation( "man", "hombre", "homme" ) ) ; array_of_translations.add( new BilingualTranslation( "sleep", "dormir" ) ) ; System.out.print( "\n Give a word to translate: " ) ; String word_to_translate = keyboard.nextLine() ; int translation_index = 0 ; while ( translation_index < array_of_translations.size() ) { array_of_translations.get( translation_index ). translate( word_to_translate ) ; translation_index ++ ; } System.out.print( "\n" ) ; } }