Question: The assignment reads as follows: Redesign and implement a version of the PigLatin program so that it uses a GUI. Accept the sentence using a
The assignment reads as follows: Redesign and implement a version of the PigLatin program so that it uses a GUI. Accept the sentence using a text field and display the results using a label. PigLatin.java //******************************************************************** import java.util.Scanner; public class PigLatin Scanner scan = new Scanner (System.in); do System.out.println (); System.out.println (); PigLatinTranslator.Java //******************************************************************** import java.util.Scanner; public class PigLatinTranslator sentence = sentence.toLowerCase(); Scanner scan = new Scanner (sentence); while (scan.hasNext()) return result; //----------------------------------------------------------------- if (beginsWithVowel(word)) return result; //----------------------------------------------------------------- char letter = word.charAt(0); return (vowels.indexOf(letter) != -1); //-----------------------------------------------------------------
// PigLatin.java Author: Lewis/Loftus
//
// Demonstrates the concept of method decomposition.
//********************************************************************
{
//-----------------------------------------------------------------
// Reads sentences and translates them into Pig Latin.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String sentence, result, another;
{
System.out.println ();
System.out.println ("Enter a sentence (no punctuation):");
sentence = scan.nextLine();
result = PigLatinTranslator.translate (sentence);
System.out.println ("That sentence in Pig Latin is:");
System.out.println (result);
System.out.print ("Translate another sentence (y/n)? ");
another = scan.nextLine();
}
while (another.equalsIgnoreCase("y"));
}
}
====================================================================
// PigLatinTranslator.java Author: Lewis/Loftus
//
// Represents a translator from English to Pig Latin. Demonstrates
// method decomposition.
//********************************************************************
{
//-----------------------------------------------------------------
// Translates a sentence of words into Pig Latin.
//-----------------------------------------------------------------
public static String translate (String sentence)
{
String result = "";
{
result += translateWord (scan.next());
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 = "";
result = word + "yay";
else
if (beginsWithBlend(word))
result = word.substring(2) + word.substring(0,2) + "ay";
else
result = word.substring(1) + word.charAt(0) + "ay";
}
// Determines if the specified word begins with a vowel.
//-----------------------------------------------------------------
private static boolean beginsWithVowel (String word)
{
String vowels = "aeiou";
}
// Determines if the specified word begins with a particular
// two-character consonant blend.
//-----------------------------------------------------------------
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") );
}
}
Step by Step Solution
3.36 Rating (149 Votes )
There are 3 Steps involved in it
import javaxswing import javaawt import javaawtevent public class PigLatin implements ActionListener ... View full answer
Get step-by-step solutions from verified subject matter experts
