Java Reference
In-Depth Information
LISTING 7.14
//********************************************************************
// PigLatinTranslator.java Author: Lewis/Loftus
//
// Represents a translator from English to Pig Latin. Demonstrates
// method decomposition.
//********************************************************************
import java.util.Scanner;
public class PigLatinTranslator
{
//-----------------------------------------------------------------
// Translates a sentence of words into Pig Latin.
//-----------------------------------------------------------------
public static String translate (String sentence)
{
String result = "";
sentence = sentence.toLowerCase();
Scanner scan = new Scanner (sentence);
while (scan.hasNext())
{
result += translateWord (scan.next());
result += " ";
}
return result;
}
//-----------------------------------------------------------------
// Translates one word into Pig Latin. If the word begins with a
// vowel, the suffix "yay" is appended 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))
Search WWH ::




Custom Search