PigLatin Lab
807607Nov 26 2006 — edited Nov 26 2006Hey i need help completing this lab.
Objectives
Demonstrate knowledge of the String and Scanner classes. Demonstrate continued knowledge of defining instantiable classes.
Problem Description:
Pig Latin is a form of coded language often used to amuse most elementary school students. Many variations exist in the methods to form Pig Latin phrases. We will use the following algorithm for simplicity.
� To change an English word into Pig Latin, place the first letter of the English word at the end of the English word and add the letters "ay" to the end. Thus, �computer� becomes �omputercay� and �wolf� becomes �olfway�. Blanks between words remain blanks. Assume the English phrase consists of words separated by blanks, there are no punctuation marks, and all words have 1 or more letters. The resultant string should have only 1 blank between each Pig Latin word.
� To change a Pig Latin word into an English word, remove the "ay" at the end and move the last letter to the front. Thus, �omputercay� becomes �computer� and �olfway� becomes �wolf�. Blanks between words remain blanks. Assume the Pig Latin phrase consists of words separated by blanks, there are no punctuation marks, and all words end in �ay� and have 3 or more letters. The resultant string should have only 1 blank between each English word.
Using the String and Scanner classes, implement the PigLatin class, and complete the PigLatinTest class as described below. The PigLatin class and PigLatinTest class have been started for you:
template:\AP Computer Science\APCS1\PigLatin.java
template:\AP Computer Science\APCS1\PigLatinTest.java
100-Point Version
PigLatin Class
Implement the following public method.
� getPiggy( String phrase) � returns a string containing the given English phrase translated into Pig Latin. Using the Scanner class, extract each word, then translate each word using the private method wordPiggy. When assembling the Pig Latin string to be returned, place only 1 space between each Pig Latin word.
Implement the following private method. Remember, a private method helps a public method to accomplish a task more easily. This method processes a single word, while the public method is responsible for the entire phrase.
� wordPiggy( String word) � returns a string containing the given English word translated into Pig Latin. Follow the algorithm outlined above.
PigLatinTest Class
You are to complete the main method by following the directions given in the first two comments inside the while loop. You are to get a phrase from the user and remove all leading and trailing blanks. The loop should stop whenever the word �STOP� has been entered, regardless of the combination of lowercase and uppercase letters. For example �stop�, �sToP �, and � stoP� would all stop the loop.
PigLatin.java
import java.util.Scanner;
public class PigLatin
{
//----------------------------------------------------------------------------
// Public Methods
//----------------------------------------------------------------------------
public String getPiggy( String phrase)
{
return ""; // replace this!
}
public String getUnpiggy( String phrase)
{
return ""; // replace this!
}
//----------------------------------------------------------------------------
// Private Method
//----------------------------------------------------------------------------
private String wordPiggy( String word)
{
return ""; // replace this!
}
private String wordUnpiggy( String word)
{
return ""; // replace this!
}
}
PigLatinTest.java
import java.util.Scanner;
public class PigLatinTest {
// class constant to hold the stopping string
private static final String STOP = new String( "STOP");
public static void main( String[] args)
{
Scanner scan = new Scanner( System.in);
PigLatin piggy = new PigLatin();
String phrase = new String();
String inPiggy;
boolean done = false;
while ( !done) {
// Ask the user for a phrase to decode, storing their response
// in the variable phrase and trim all leading/trailing blanks
// Stop when they enter "stop" in any form of upper/lowercase
// if ()
// {
// done = true;
// }
// else
{
// Encode the phrase and decode the phrase
inPiggy = piggy.getPiggy( phrase);
System.out.println( "\nEncoded:\n" + inPiggy);
/* Uncomment for 110-point version
System.out.println( "\nDecoded:\n" +
piggy.getUnpiggy( inPiggy));
*/
System.out.println();
}
}
System.out.println();
}
}
Message was edited by:
piglatin