Question
Java String help I have finished about 70% of the homework. I just need someone to fillout one method (Please fill out the Translate method!)
Java String help
I have finished about 70% of the homework. I just need someone to fillout one method(Please fill out the "Translate method"!) and fill out one file please (KeyboardApp.Java). It shouldn't take too long since I have finished all other methods. Following are the requirements:
On your own you need to finish the two remaining methods of StringUtilities, firstVowel and translate. You will also need to set up KeyboardApp.java so it can take in strings from the command line as the user types them and then translate them. When the user types quit (without the quotes) the program should end. You can do this all in main. So you know, to translate to Pig Latin, take a String and find the index of the first vowel. If the index is not -1 then you will create two substrings, the letters before the first vowel (call it start) and the letters from the first vowel to the end (call it end). The translation is: last + first + ay. If the index is -1 then return: word + ay.
Here are the codes I have finished.
public class StringUtilities { //----------------------------------------------------------------- /** return index of first vowel. * returns -1 if the word has no vowels * @param word String * @return int the index of the first vowel. */ public static int firstVowel( String word ) { for(int i = 0; i < word.length();i++) { char c = word.charAt(i); if (StringUtilities.isVowel(new Character(c).toString())) { return i; } } return -1; } //----------------------------------------------------------------- /** returns true if the string represents a vowel (a,e,i,o or u) * otherwise return false. * @param letter String * @return boolean */ public static boolean isVowel( String letter ) { if(letter.equalsIgnoreCase("a")||letter.equalsIgnoreCase("e")||letter.equalsIgnoreCase("i") ||letter.equalsIgnoreCase("o")||letter.equalsIgnoreCase("u")) { return true; } return false; } //--------------------------------------------------------------- /** returns a String representing the letter in "text" at index n * returns null if the index is out of bounds. * @param text String * @param n int * @return String */ public static String letterAt ( String text, int n ) { if (n=0) { char letter = text.charAt(n); return new Character(letter).toString(); } return null; } //----------------------------------------------------------------- /** returns "word" translated to pig latin. * * @param word String * @return String */ public static String translate( String word ) //FILL THIS PART PLEASE! { if(StringUtilities.firstVowel(new String(word)) == -1) { return word+"ay"; } else } } //--------------------------------------------------------------- /** A unit test of the above methods. * DO NOT EDIT * * @param args String */ public static void main( String args[] ) { String alpha; //-------------------------------------------------------------- alpha = "ABCDE"; System.out.println( " ******** TEST LetterAt ************" ); System.out.println( "Original: " + alpha ); String letter; for( int i = -1; i < alpha.length() + 1; i++ ) { letter = letterAt( alpha, i ); if( letter == null ) System.out.println( "LetterAt " + i + " : " + "null" ); else System.out.println( "LetterAt " + i + " : " + letter ); } //------------------------------------------------------------- alpha = "ABCDEioux"; System.out.println( " ******** TEST isVowel *************" ); System.out.println( "Word: " + alpha ); for( int i = 0; i < alpha.length(); i++ ) { letter = letterAt( alpha, i ); if( letter == null ) System.out.println( "LetterAt " + i + " : " + "null" ); else System.out.println( "isVowel " + letter + " is " + isVowel( letter ) ); } //-------------------------------------------------------------- System.out.println( " ******** TEST firstVowel **********" ); String words[] = { "one", "two", "three", "shhh", "hymn", "" }; for( int i = 0; i < words.length; i++ ) { int idx = firstVowel( words[ i ] ); if( idx == -1 ) System.out.println( words[ i ] + ": no vowels" ); else System.out.println( words[ i ] + " : '" + letterAt( words[ i ], idx ) + "' at index " + idx ) ; } //-------------------------------------------------------------- System.out.println( " ******* TEST translator ***********" ); String words2[] = { "dog", "bananna", "nix", "scram" }; for( int i = 0; i < words2.length; i++ ) System.out.println( words2[ i ] + " : " + translate( words2[ i ] ) ); } // end of main } // end of class
KeyboardApp.Java(PLEASE FILL OUT THIS CLASS!) import java.util.*; import java.awt.event.*; /**-------------------------------------------------------- * KeyboardApp.java: * Pig Latin Translator * * * @author txr185 */ public class KeyboardApp { //-------------------------------------------------------- /** Creates a Frame and a KeyBoardApp. * * @param args String[] */ public static void main( String args[] ) { } }
Thank you very much!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started