The purpose of this lab is to build a program with loops. You will continue to use the other skills you have picked up over the last few weeks.


You will write a program that converts text to Pig Latin.

If you are not familiar with Pig Latin take a look at the article on Wikipedia.

As you can see from the Wikipedia article, to convert a word to Pig Latin you remove the first syllable, unless the word starts with a vowel (“a”, “e”, “i”, “o” or “u”; for our purposes here, “y” is not a vowel). Then put this first syllable at the end of the word and add the suffix “ay”. For example the first syllable of the word “stop” is “st” the rest of the word is “op”, so put the first syllable at the end and you get “op-st” then add “ay” to get “op-st-ay” and you have the pig latin translation of “stop” is “opstay”.

For our purposes we will define the first syllable as all leading consonants. If there are no initial consonants, then there is nothing to move to the end, but you still add “ay”, and in this way “is” which becomes “isay”.

Furthermore, your program should continue asking for text to translate until the user asks to stop.

The output should look something like:

Please give me some text to convert into Pig Latin (use the word "opstay" to stop): i wrote this program because i can not speak pig latin
i wrote this program because i can not speak pig latin isay:

	iay otewray isthay ogrampray ecuasebay iay ancay otnay eakspay igpay atinlay 

inay igpay atinlay!

Please give me some text to convert into Pig Latin (use the word "opstay" to stop): while this is fun, i think i will go do  something more fun for labor day!
while this is fun, i think i will go do something more fun for labor day! isay:

	ilewhay isthay isay unfay, iay inkthay iay illway ogay oday omethingsay oremay unfay orfay aborlay ayday! 

inay igpay atinlay!

Please give me some text to convert into Pig Latin (use the word "opstay" to stop): opstay

Ok, see you later!

Please note that I correctly handled the “,” and “!” at the end of the words “fun” and “day”. I did NOT produce “un,fay”. You only need to handle a single punctuation mark at the end of the word; don't worry about punctuation at the beginning or multiple marks.

For extra credit you can handle input with punctuation at the beginning of a word, like : 'run', he said. which should translate to : 'unray', ehay aidsay. (the leading quotation mark is preserved)

As you did in the last lab, write some test cases, that is, sentences and corresponding translations. These should starting very small and simple but progressively become more complex. You will use these to test that your program does what it is supposed to.

Useful Tips and Methods

Java has some very nice tools for extracting words from a sentence and letters from a word. An API (Application Programming Interface) is a site that contains documentation about methods, their input parameters, their return values, etc. If you ever catch yourself wondering if a method exists to do a simple task, chances are it does and you'll find it in the API. Java has an API to document methods for java classes. Consider some of the following methods in the Java API:

Methods for a string object:

Methods for a Scanner object:

Note also that a Scanner can be initialized with both “System.in” (for user input) and with a string object (to scan through a string).

Another useful tip is that you can combine multiple logic statements into a single logic statement. See section 3.5 of your text.

Think before programming

For the rest of the class you will be constantly having to think about how to break the problem into smaller pieces and to solve the pieces one at a time.

In this case, try first building a program that will read in lines and print them back out until the user types “opstay” (see sentinel values in the book). “.nextLine” on a scanner will give you the next whole line as a string. You just need to ask for lines until you get one that is equal to “opstay”.

Once you have a line, you need to break it into words. One way to do this is with another scanner. Make a new scanner inside of your “outer loop”. Unlike the scanner that gets text from System.in, you will ask this scanner to read from the very line you just got from the user. Sort of like this:

<syntaxhighlight lang=“java” line=“GESHI_FANCY_LINE_NUMBERS”> import java.util.Scanner;

// other stuff

Scanner lineScanner = new Scanner(System.in); lineFromUser = lineScanner.nextLine(); // some sort of loop

    Scanner wordScanner = new Scanner(lineFromUser);
    // more stuff

wordScanner.next() gets the next word from the lineFromUser and wordScanner.hasNext() tells you that there are still words to be process on this line. Your book only makes a short comment about using a scanner this way. You many have some other questions, but try to use eclipse to answer them. I do not remember everything about how a scanner works, neither should you.

Again, take this step by step. You should now be able to read in the line and print it back out word by word.

Last you will have to find any trailing punctuation, separate it out, find the leading consonants (another loop! see page 138). Then reassemble the word (see the top of page 64), assemble a line of words and print it all out. I will let you think about how to break these parts up. Do one little piece at a time, printing out what you have so far.

Getting Help

If you get stuck you might try thinking about the problem in the order opposite from what I suggested above. That is, try to make a program that reads in a single word and translates it. Then a program that takes in a single line and translates each word, and finally a program that reads multiple lines as show above. This approach is called “bottom-up” because we start with the smallest, most basic step in the program and then build up from there.

The approach given in the previous section is called “top-down”, it starts with the “broad strokes” first, ignoring the fact that we do not yet know how to manipulate the most basic elements (the words in this case) yet. This approach seeks first to just to isolate them (the words).

Some programmers will do a little of both, sometimes thinking first top-down, but then writing code bottom-up. I find it useful to start coding top-down, but I write little separate test programs to make sure I can see how to do the bottom (basic) pieces too. Last I combine the pieces I have. We are all different, you will need to find your own way to think about problems and to actually write programs, but I encourage you to try to get parts working before you expect the whole program to work.

Try to discipline yourself to get some parts working before you see a TA. You might find that once something is working, that you can see how to get the next part working!

Pass-off procedure

When you have your program working, you will need to show it to a TA. The TA will evaluate your code based on the following criteria:

  1. Show the TA your test cases, with the input and correct output (manually written). You will be graded on the diversity of your tests. You should have about 5-10 tests. Note that you get these points for just writing the test cases, no programming required. (5 points)
  2. As you start to write a program to do the conversion, start with small simple steps as listed above. Note that you do not have to show the TA each step individually. The TA will simply check that your program accomplishes each of these elements:
    1. Ask for text to convert until the user responds with “opstay” (5 points)
    2. Isolate each word in the text given by the user (5 points)
    3. Convert words that start with a vowel (that means you just add “ay”) (5 points)
    4. Convert words that start with one consonant (5 points)
    5. Convert words that start with two or more consonants (3 points)
    6. Convert words that are followed by a punctuation mark (anything that is not a letter) (3 points)
  3. Is your code properly formatted (indenting blocks, comments, spacing, reasonable names of variables)? (5 points)

2 points extra credit: Preserve capitalization at the beginning of words (“Kevin” → “Evinkay”, not “evinKay”)

Additional 2 points extra credit: Handle multiple punctuation marks at the beginning and ending of words correctly ( 'Hey', 'Eyhay', and not Hey','ay)

cs-142/fall-2010/labs/simple-pig-latin.txt · Last modified: 2014/11/19 15:51 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0