chapter_15_arraylists_javabook

Add new trilingualtranslation woman mujer femme

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: you must use a backslash \ before the double quote character. Translate.java - 2: Class TrilingualTranslation and its version of method translate(). 511 512 Chapter 15: More standard Java types Here, translation objects are created to the heap memory, and references to the objects are added to the end of array_of_translations. An ArrayList-based array is used to store references to translation objects. When an ArrayList array is created this way, the array can store references to objects that are either objects of class BilingualTranslation or objects of some subclass of BilingualTranslation. As TrilingualTranslation is a subclass of BilingualTranslation, TrilingualTranslation objects can be stored as well. class Translate { public static void main( String command_line_parameters ) { ArrayList<BilingualTranslation> array_of_translations = new ArrayList<BilingualTranslation>() ; 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" ) ) ; if ( command_line_parameters.length { int translation_index = 0 ; == 1) while ( translation_index < array_of_translations.size() ) { array_of_translations.get( translation_index ). translate( command_line_parameters[ 0 ] ) ; translation_index ++ ; } System.out.print( "\n" ) ; } else { System.out.print( "\n Give a word on command line.\n\n" ) ; } } } Method translate() is called here to possibly produce a translation. Depending on what type of object is referenced by the array element of array_of_translations, the appropriate version of the two versions of method translate() is selected automatically. Translate.java - 3. A simple translation application which uses the translation classes. 15.1 ArrayList class 513 keew etalsnarT avaj>3selifavaj\:D "anames" ot setalsnart "keew" In this case the given word was found as the last word inside an object of type namow etalsnarT avaj>3selifavaj\:D "emmef" dna "rejum" ot setalsnart "namow" TrilingualTranslation. eur etalsnarT avaj>3selifavaj\:D "ellac" dna "teerts" ot setalsnart "eur" Translate.java - X. The program is executed three times here. Exercises with program Translate.java Exercise 15-1. Modify program Translate.java so that it informs the user if it is not able to translate the given word. Method translate() returns true or false depending on whether or not the translation was successful, but in the current versi...
View Full Document

Ask a homework question - tutors are online