// TranslateSolutions.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-23 File created. // 2006-02-23 Last modification. // Solutions to exercises 15-1 and 15-2. 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() {} 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 FourLanguageTranslation extends TrilingualTranslation { protected String fourth_word ; public FourLanguageTranslation( String given_first_word, String given_second_word, String given_third_word, String given_fourth_word ) { first_word = given_first_word ; second_word = given_second_word ; third_word = given_third_word ; fourth_word = given_fourth_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 + "\", \"" + third_word + "\" and " + fourth_word + "\"") ; translation_was_successful = true ; } if ( given_word.equals( second_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\", \"" + third_word + "\" and " + fourth_word + "\"") ; translation_was_successful = true ; } if ( given_word.equals( third_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\", \"" + second_word + "\" and " + fourth_word + "\"" ) ; translation_was_successful = true ; } if ( given_word.equals( fourth_word ) ) { System.out.print( "\n \"" + given_word + "\" translates to \"" + first_word + "\", \"" + second_word + "\" and " + third_word + "\"" ) ; translation_was_successful = true ; } return translation_was_successful ; } } class TranslateSolutions { public static void main( String[] command_line_parameters ) { 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" ) ) ; array_of_translations.add( new FourLanguageTranslation( "car", "coche", "voiture", "auto") ) ; array_of_translations.add( new FourLanguageTranslation( "country", "pais", "pays", "land") ) ; if ( command_line_parameters.length == 1 ) { boolean word_has_been_translated = false ; int translation_index = 0 ; while ( translation_index < array_of_translations.size() ) { if ( array_of_translations.get( translation_index ). translate( command_line_parameters[ 0 ] ) == true ) { word_has_been_translated = true ; } translation_index ++ ; } if ( word_has_been_translated == false ) { System.out.print( "\n Could not translate \"" + command_line_parameters[ 0 ] + "\"" ) ; } System.out.print( "\n" ) ; } else { System.out.print( "\n Give a word on command line.\n\n" ) ; } } }