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.
StringTokenizer classStringTokenizer 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.
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!";
}
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!";
}
}
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.
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.
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.
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".
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.