Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Thursday Lab #8: String methods
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 2:59 pm on Alloweenhay

The class exercises from Wednesday (10/29/2014) are relevant to this assignment. You'll see that I've linked a copy of my solution on the Class Notes for that day.

Pig Latin

Write a program that translates text entered by the user into Pig Latin. The program should prompt for a phrase, translate the contents to Pig Latin, and write the result to a new file.

There are several levels of achievement for this lab. See the Standards section, below.

Basic rules for Pig Latin

The basic translation to Pig Latin is pretty simple: if a word begins with a consonant sound, move all "consonant" letters to the end of the word and add the letters "ay". If a word begins with a vowel sound, simply add the letters "yay" to the end of the word. For example,

Good morning! How are you today?

in Pig Latin is

Oodgay orningmay! Owhay areyay ouyay odaytay?

Similarly, the text

Wit, and't be thy will, put me into good fooling! Those wits
that think they have thee do very oft prove fools; and I, that am
sure I lack thee, may pass for a wise man. For what says
Quinapalus? Better a witty fool than a foolish wit.--God bless
thee, lady!

should become

Itway, and'tyay ebay ythay illway, utpay emay intoyay oodgay oolingfay! Osethay itsway
atthay inkthay eythay avehay eethay oday eryvay oftyay ovepray oolsfay; andyay Iyay, atthay amyay
uresay Iyay acklay eethay, aymay asspay orfay ayay iseway anmay. Orfay atwhay ayssay
Inapalusquay? Etterbay ayay ittyway oolfay anthay ayay oolishfay itway.--Odgay essblay
eethay, adylay!

The basic vowels are: 'a', 'e', 'i', 'o', and 'u'. The main refinements arise from the exceptions to the "vowels list". After all, the list should really be: 'a', 'e', 'i', 'o', sometimes 'u' (when it isn't immediately following a 'q'), and sometimes 'y' (when it occurs after the first letter). For example:

Yahoo! Qasida quells quiet rhythms!


Ahooyay!  Asidaqay ellsquay ietquay ythmsrhay!

How to Solve This

Focus first on getting the basic I/O structure down for the program: prompt for the phrase, read it, and write its contents, untranslated, to standard output. That's your main() method.

Next, concentrate on writing the Pig Latin translation functions. You will want at least three methods, plus a main() method:

public class PigLatin {
    public static void main(String[] args) { ... }
    public static String translate(String sen) { ... }
    public static String translateWord(String word) { ... }
    public static int indexOfFirstVowel(String word) { ... }

You'll use translate() in the body of main(), translateWord() in the body of translate(), and indexOfFirstVowel() in the body of translateWord(). Test each one by using DrJava's interactive interpreter.

The translate() function takes a sentence, which may have any amount of whitespace and/or punctuation, in addition to the words, and returns that sentence in Pig Latin, with all spacing and punctuation preserved. As a first effort, just assume that the input will be in lower case letters, with no punctuation (see Standards, below). For the necessary techniques in handling punctuation and capitalization, study the capitalizeAll() and talkBackwards() functions in

If you have removed all punctuation and rendered the word in lower case, the translateWord() function is pretty easy: simply find the index of the first vowel, use that to split the word, reverse the halves, and add either "ay" or "yay" at the end, as appropriate.

Of the three, indexOfFirstVowel() is probably the most challenging. You will need to find the index in the word where the first "functional" vowel occurs. For the first character, that could be any of the "basic" vowels. For any character after the first, the vowels will also include 'y'. If the word starts with a sequence of consonants that end in 'q', however, then the subsequent 'u' should also be moved to the end: i.e., 'u' is not a vowel in this context, though other letters may be: "qiviut" becomes "iviutqay", for example, while "squiggly" becomes "igglysquay".

If that's a bit overwhelming, start with a simpler version that only solves the basic "aeiou" rule, ignoring the 'y' and "qu" exceptions completely. Then go back and add these refinements

Here's a hint for one approach to this (there are others). List the vowels as a String:

String vowels = "aeiou";

You can use this to check each character in a word for membership in the "vowels" list, starting at the beginning, until you find a vowel. After the second character, add "y" to the vowels list. The charAt and indexOf methods that come with every String object will be handy here.


Levels of Completeness

The basic version of this problem is pretty easy: we assume that the input consists only of lower-case words, with no punctuation of any kind. In this approach, our main() method prompts for input and passes that to translate(). In translate(), this input is split into individual words, and for each one, we call translateWord(), with the result appended to an overall Pig Latin phrase. When that's done, translate() returns this Pig Latin phrase, and the result is printed out in main(). You can simplify translation, too, by ignoring the "y" and "qu" problems. Even more radically, you can just append "ay" onto every word, whether it starts with vowels or consonants.

Formatting Requirements

See the Style Guide, available from the General Notes section of our course web site. All elements of beautiful, clear code style described there will be expected.


Turn In

John H. E. Lasseter