next up previous
Next: Problem Design: The PigLatin Up: In the Laboratory: Pig Previous: Problem Decomposition

The Algorithm

The algorithm for this lab will solve the following problem: Given an English sentence or expression -- a string of words separated by blanks -- translate the string word by word into Pig Latin. For the sake of simplicity, let's leave off all punctuation symbols. The algorithm should go through the string word by word, and for each word, it should translate it into Pig Latin and concatenate it to the result string. As we know, in order to translate a word into Pig Latin, we must find the location of its first vowel and then follow the above translation rules. This suggests the following algorithm, which could be encapsulated into the translate() method:

    Initialize a result string
    For each word in the input string      // String tokenizer task
        translate it into Pig Latin        // translateWord task
        Append it to the result string     // String concatenation task
    Return the result string

As the comments suggest, this algorithm can be broken up into subtasks. The first subtask is to get each word out of the input string. This is a perfect job for the StringTokenizer class discussed above. The second subtask is to translate a single word into Pig Latin. This task is substantial enough to be encapsulated into a separate method, the translateWord() method. Lastly, string concatenization is easily done by using the ``+'' operator.

The translateWord() Method. To translate a word into Pig Latin, you must find the location of its first vowel ('a', 'e', 'i', 'o', or 'u') and then apply the above rules. If the word begins with a vowel (or doesn't contain a vowel) you simply append ``yay'' to the end of the word -- ``able'' becomes ``ableyay'' and ``my'' becomes ``myyay.'' Otherwise you divide the word into substrings with the first vowel becoming the first letter of the Pig Latin word and any letters preceding it being appended to the end of the word and followed by ``ay'' -- ``string'' becomes ``ing'' + ``str'' + ``ay'' or ``ingstray.''

The findFirstVowel() Method. The task of finding the first vowel in a string is also a good candidate for encapsulation into a separate method. It takes an English word as its String parameter and returns an int giving the index location of the first vowel.

If the word does not contain a vowel -- for example, ``my'' -- the method should return 0. For example, findFirstVowel("hello") should return 1 as the location of 'e', and findFirstVowel("able") should return 0, and findFirstVowel("my") should also return 0. The reason for having it return 0 in two different cases is that in both cases you handle the translation in the same way -- ``able'' becomes ``ableyay'' and ``my'' becomes ``myyay.'' In other words, according to the Pig Latin rules, there's no difference between a word that begins with a vowel and one that doesn't contain a vowel.


next up previous
Next: Problem Design: The PigLatin Up: In the Laboratory: Pig Previous: Problem Decomposition
Ralph Morelli {Faculty}
12/22/1999