Change the PigLatinTranslator class so that the translate method is static. Chan
ID: 3657949 • Letter: C
Question
Change the PigLatinTranslator class so that the translate method is static. Change the PigLatin class so it invokes the method correctly. Please explain how to do it too. PigLatinTranslator.java: public class PigLatinTranslator { // Translates a sentence of words into Pig Latin using String // methods toLowerCase and split // Note: split not in the AP subset! public String translate(String sentence) { String result = ""; sentence = sentence.toLowerCase(); String[] holdPieces = sentence.split("\s"); for(int i = 0; i < holdPieces.length; i++) { result += translateWord(holdPieces[i]); result += " "; } return result; } /** The following methods are declared private. They are not accessed * by client outside the class. They only to help the translate * method. They cannot be called outside of this class. */ // Translates one word into Pig Latin. If the word begins with a vowel, // the suffix "yay" is appened to the word. Otherwise, the first letter // or two are moved to the end of the word, and ay is appended. private String translateWord(String word) { String result = ""; if(beginsWithVowel(word)) result = word + "yay"; else if(beginsWithBlend(word)) result = word.substring(2) + word.substring(0, 2) + "ay"; else result = word.substring(1) + word.substring(0,1) + "ay"; return result; } // Determines if the specified word begins with a vowel private boolean beginsWithVowel(String word) { String vowels = "aeiou"; String letter = word.substring(0, 1); return (vowels.indexOf(letter) != -1); } // Determines if the specified word begins with a particular // two-character consonant blend // Uses the String method startsWith // Note: startsWith not in the AP subset! private boolean beginsWithBlend(String word) { return(word.startsWith("bl") || word.startsWith("sc") || word.startsWith("br") || word.startsWith("sh") || word.startsWith("ch") || word.startsWith("sk") || word.startsWith("cl") || word.startsWith("sl") || word.startsWith("cr") || word.startsWith("sn") || word.startsWith("dr") || word.startsWith("sm") || word.startsWith("dw") || word.startsWith("sp") || word.startsWith("fl") || word.startsWith("sq") || word.startsWith("fr") || word.startsWith("st") || word.startsWith("gl") || word.startsWith("sw") || word.startsWith("gr") || word.startsWith("th") || word.startsWith("kl") || word.startsWith("tr") || word.startsWith("ph") || word.startsWith("tw") || word.startsWith("pl") || word.startsWith("wh") || word.startsWith("pr") || word.startsWith("wr")); } } PigLatin.java: // Driver to exercise the PigLatinTranslator class import java.util.Scanner; public class PigLatin { // Reads sentences and translates them into Pig Latin public static void main(String[] args) { String sentence, result, another = "y"; Scanner input = new Scanner(System.in); PigLatinTranslator translator = new PigLatinTranslator(); while(another.equalsIgnoreCase("y")) { System.out.println(); System.out.print("Enter a sentence (no punctuation): "); sentence = input.nextLine(); System.out.println(); result = translator.translate(sentence); System.out.print("That sentence in Pig Latin is: "); System.out.println(result); System.out.println(); System.out.print("Translate another sentence? (y/n) "); another = input.nextLine(); } } }Explanation / Answer
Make translate() static, which requires making all helper methods for translate() static. Then in main(), there is no need to create an instance of PigLatinTranslator.
public class PigLatinTranslator
{
// Translates a sentence of words into Pig Latin using String
// methods toLowerCase and split
// Note: split not in the AP subset!
public static String translate(String sentence)
{
String result = "";
sentence = sentence.toLowerCase();
String[] holdPieces = sentence.split("\s");
for(int i = 0; i < holdPieces.length; i++)
{
result += translateWord(holdPieces[i]);
result += " ";
}
return result;
}
/** The following methods are declared private. They are not accessed
* by client outside the class. They only to help the translate
* method. They cannot be called outside of this class.
*/
// Translates one word into Pig Latin. If the word begins with a vowel,
// the suffix "yay" is appened to the word. Otherwise, the first letter
// or two are moved to the end of the word, and ay is appended.
private static String translateWord(String word)
{
String result = "";
if(beginsWithVowel(word))
result = word + "yay";
else if(beginsWithBlend(word))
result = word.substring(2) + word.substring(0, 2) + "ay";
else
result = word.substring(1) + word.substring(0,1) + "ay";
return result;
}
// Determines if the specified word begins with a vowel
private static boolean beginsWithVowel(String word)
{
String vowels = "aeiou";
String letter = word.substring(0, 1);
return (vowels.indexOf(letter) != -1);
}
// Determines if the specified word begins with a particular
// two-character consonant blend
// Uses the String method startsWith
// Note: startsWith not in the AP subset!
private static boolean beginsWithBlend(String word)
{
return(word.startsWith("bl") || word.startsWith("sc") || word.startsWith("br") || word.startsWith("sh") || word.startsWith("ch") || word.startsWith("sk") || word.startsWith("cl") || word.startsWith("sl") || word.startsWith("cr") || word.startsWith("sn") || word.startsWith("dr") || word.startsWith("sm") || word.startsWith("dw") || word.startsWith("sp") || word.startsWith("fl") || word.startsWith("sq") || word.startsWith("fr") || word.startsWith("st") || word.startsWith("gl") || word.startsWith("sw") || word.startsWith("gr") || word.startsWith("th") || word.startsWith("kl") || word.startsWith("tr") || word.startsWith("ph") || word.startsWith("tw") || word.startsWith("pl") || word.startsWith("wh") || word.startsWith("pr") || word.startsWith("wr"));
}
}
// Driver to exercise the PigLatinTranslator class
import java.util.Scanner;
public class PigLatin
{
// Reads sentences and translates them into Pig Latin
public static void main(String[] args)
{
String sentence, result, another = "y";
Scanner input = new Scanner(System.in);
while(another.equalsIgnoreCase("y"))
{
System.out.println();
System.out.print("Enter a sentence (no punctuation): ");
sentence = input.nextLine();
System.out.println();
result = PigLatinTranslator.translate(sentence);
System.out.print("That sentence in Pig Latin is: ");
System.out.println(result);
System.out.println();
System.out.print("Translate another sentence? (y/n) ");
another = input.nextLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.