chapter_15_arraylists_javabook

In the derived class trilingualtranslation there is a

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: va.util.ArrayList ; 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 ; } } Method translate() is a polymorphic method of which there exist several versions in this class hierarchy. In the derived class TrilingualTranslation there is a different version of this method. Translate.java - 1: The declaration of class BilingualTranslation. This method returns true if it can translate the given word. Translation is possible if the given word is the same as some of the words inside the object itself. A line of text is printed only if translation is possible. 15.1 ArrayList class Being an enhanced version of its superclass, class TrilingualTranslation works with three natural words. The words are supplied to the constructor when a translation object is created. class TrilingualTranslation extends { protected String third_word ; BilingualTranslation 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 ; } } Because this method works with three natural languages, it prints longer lines of text than the corresponding method in class BilingualTranslation. Note that if you want to include double quote characters inside a string literal,...
View Full Document

Ask a homework question - tutors are online