chapter_15_arraylists_javabook

Both arrays contain characters and their morse codes

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: , after this statement has been executed, the ArrayList array is an ArrayList version of the conventional array. Both arrays contain characters and their Morse codes as strings. The Morse code of a character is always in the following array position. import java.util.* ; class MorseCodes { public static void main( String not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; String { "A", "F", "K", "P", "U", "Z", "5", "0", array_of_morse_codes ".-", "B", "..-.", "G", "-.-", "L", ".--.", "Q", "..-", "V", "--..", "1", ".....","6", "-----"," ", = "-...", "C", " --.", "H", ".-..", "M", "--.-", "R", "...-", "W", ".----","2", "-....","7", " " }; "-.-.", "D", "....", "I", "--", "N", ".-.", "S", ".--", "X", "..---","3", "--...","8", "-..", "E", ".", "..", "J", ".---", "-.", "O", "---", "...", "T", "-", "-..-","Y", " -.--", "...--","4","....-", "---..","9","----.", ArrayList<String> arraylist_of_morse_codes = new ArrayList<String>() ; Collections.addAll( arraylist_of_morse_codes, array_of_morse_codes ) ; System.out.print( "\n Type in your name: " ) ; String keyboard.nextLine().toUpperCase() ; given_name = System.out.print( "\n Your name in Morse codes is: \n\n" ) ; for ( int character_index = 0 ; character_index < given_name.length() ; character_index ++ ) { int index_of_character_in_arraylist = arraylist_of_morse_codes.indexOf( "" + given_name.charAt( character_index ) ) ; if ( index_of_character_in_arraylist != -1 ) { System.out.print( " "+ arraylist_of_morse_codes.get( index_of_character_in_arraylist + 1 ) ) ; } } } } Because the Morse code of a character always follows the character in the array, we add one to the index in order to get the Morse code. MorseCodes.java - 1. Using class ArrayList to store String objects. 509 510 Chapter 15: More standard Java types This program can make translations between words of two or three natural languages. BilingualTranslation is a class that is used to translate words between two natural languages like English and Spanish. Objects of class BilingualTranslation contain a pair of words which translate to each other. // Translate.java (c) Kari Laitinen Here, there must be a default constructor (i.e. a constructor which can be called without giving any parameters) because another class is derived from this class. The default constructor of this class is executed before the constructor of the derived class. import ja...
View Full Document

Ask a homework question - tutors are online