AP Java Assignment 7
PigLatin

Background: Pig Latin

Pig Latin is a language game where words are "translated" according to the following rules:
  1. For words that are all consonants, simply add "ay" to the end of the word. Thus, "try" becomes "tryay".
  2. For words that begin with vowels, simply add "way" to the end of the word. Thus, "a" becomes "away"; "at" becomes "atway"; "ermine" becomes "ermineway."
  3. For words beginning with "qu," move the "qu" to the end of the word and add ay. Thus "question" becomes "estionquay".
  4. For words that begin with consonants, move the leading consonant(s) to the end of the word and add "ay." Thus, "ball" becomes "allbay"; "button" becomes "uttonbay"; "star" becomes "arstay"; "three" becomes "eethray";

There are many dialects and forms of Pig Latin which vary from region to region, country to country, and language to language, as well as other similar Pig Latin-like 'languages'. The dialect shown here is common in California and the west coast of the United States.

The StringTokenizer class

A StringTokenizer does exactly what your applet in assignment 6 does; it parses a String into individual words (called "Tokens"). Experiment with the program outline below, and read about the StringTokenizer class in the Java API to familiarize yourself with how this class works before starting this assignment.

What you need to do

Using the StringTokenizer class saves us a lot of work. The only thing you really need to do is to finish the following two methods:

   int findFirstVowel(String sWord)
   //precondition: sWord is a valid String of length greater than 0.
   //postcondition: returns the position of the first vowel in sWord.  If there are no vowels, returns -1
   {
      return -1;
   }
            
   String pigLatin(String sWord)
   //precondition: sWord is a valid String of length greater than 0
   //postcondition: returns the pig latin equivalent of sWord
   {
      if(findFirstVowel(sWord) == -1)
         return sWord + "ay";
      else
         return "ERROR!";
   }

Steps to completing this assignment:

  1. Start by modifying the following program outline:
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Apjava7 extends Applet implements ActionListener
    {
       TextArea ta;
       Button translate;
    
       public void init()
       {
          ta = new TextArea("",10,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
          translate = new Button("translate into pig latin");
          add(ta);
          add(translate);
          translate.addActionListener(this);
       }
       public void paint(Graphics g)
       {
    	   
       }
       public void actionPerformed(ActionEvent e)
       {
          String sInput = ta.getText();
          String sOutput = new String("");
          ta.setEditable(false);
          StringTokenizer st = new StringTokenizer(sInput);
          while (st.hasMoreTokens()) 
          {
             sOutput+=pigLatin(st.nextToken())+' ';
          }       
          ta.setText(sOutput);
          repaint();
       }
       int findFirstVowel(String sWord)
       //precondition: sWord is a valid String of length greater than 0.
       //postcondition: returns the position of the first vowel in sWord.  If there are no vowels, returns -1
       {
          return -1;
       }
                
       String pigLatin(String sWord)
       //precondition: sWord is a valid String of length greater than 0
       //postcondition: returns the pig latin equivalent of sWord
       {
          if(findFirstVowel(sWord) == -1)
             return sWord + "ay";
          else
             return "ERROR!";
       }
    
    }
  2. Complete the method int FindFirstVowel(String sWord). It returns the position of the first 'a', 'e', 'i', 'o' or 'u'. If the word contains no vowels (like "try"), the method should return -1.
  3. Modify the pigLatin() method to implement the four rules of pig latin shown above. Rule 1 has already been implemented for you. Don't worry about capitals or punctuation yet.
  4. Modify pigLatin() so that if the last character of sWord is not a letter of the alphabet, it remains the last character. For instance, "quit." will be "itquay." keeping the period at the end of the word. You will probably want to use isLetter from the Character wrapper class.
  5. Modify pigLatin() so that if the first letter of sWord is a capital, it returns a word with a capitalized first letter. For instance "Horse" would be "Orsehay".
  6. Your applet should now work correctly. Try translating the following passage:

    The Lowell Hymn:

    With heads bared we stand,
    In tribute to thee,
    Our Alma-Mater Lowell,
    All true to thee we'll be.

    Unfurled red and white,
    None shall thee decry,
    They name we love,
    Oh Lowell High.

    The Lowell Hymn in Pig Latin:

    Ithway eadshay aredbay eway andstay,
    Inway ibutetray otay eethay,
    Ourway Alma-Materway Owelllay,
    Allway uetray otay eethay e'llway ebay.

    Unfurledway edray andway itewhay,
    Onenay allshay eethay ecryday,
    Eythay amenay eway ovelay,
    Ohway Owelllay Ighhay.

You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.

Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message to mrsimon@lycos.com.