Lab 6: Pig Latin Converter

In this lab you will design a Java applet for converting text to Pig Latin. You should first design your applet to convert a single word to a Pig Latin word. After you accomplish this, you should modify your code to convert an entire sentence to Pig Latin.

You have until 2:00 pm on Wednesday, June 11 to complete this lab.

Preliminaries

Create a subdirectory of your public_html/cps001 directory to store your applet files for this lab. Access the P: drive from My Computer on your desktop. Open the folder for your public_html directory. Open the folder for the cps001 directory you just made. Create a new folder titled Lab6.

Create an HTML document for your applet. Start GNU Emacs. Go to File->Open and open a new file in your Lab6 directory named PigLatin.html. Type in the complete HTML page for your applet. In the applet tag set width = 500 and height = 175.

Save your PigLatin.html file.

Download the file PigLatin.java to your Lab6 directory as follows:

Open PigLatin.java in Emacs.

Pig Latin Applet

Your applet should look and function like this:

Applet fields and buttons

You need to declare, initialize, and add the following fields and buttons to your applet:

Subroutines

int FindFirstVowel(String s)

You will be using the subroutine FindFirstVowel that we discussed in class. The subroutine FindFirstVowel takes a String as its argument and returns an int that represents the location of the first vowel.

Examples:
int i = FindFirstVowel("Java"); (i = 1)
int i = FindFirstVowel("object"); (i = 0)
int i = FindFirstVowel("primitive"); (i = 2)
int i = FindFirstVowel("why"); (i = -1)

You use this integer to determine how to convert the string to Pig Latin. The complete Java code for the FindFirstVowel subroutine is below.

int FindFirstVowel(String word) { char c; int index = 0; word = word.toLowerCase(); while( index < word.length() ) { c = word.charAt(index); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return index; index++; } return -1; }

String Convert(String s)

You will be finishing the subroutine Convert that we discussed in class. The subroutine Convert takes a String as its argument and returns the Pig Latin conversion as a String. There are three possible cases to consider. The value returned by FindFirstVowel is used to determine how to convert the word to Pig Latin (which case applies). The partially written Java code for the Convert subroutine is below. You need to add the code for the various cases as indicated. Remember that in each case you need a return statement. The substring method would likely be something you want to use!

String Convert(String s) { int i = FindFirstVowel(s); /* Case 1: the word begins with a vowel */ if( i == 0 ) { /* Add your code for Case 1 here */ } /* Case 2: the word begins with consonant but has a vowel */ else if( i > 0 ) { /* Add your code for Case 2 here */ } /* Case 3: the word has no vowels */ else { /* Add your code for Case 3 here */ } }

What to do in actionPerformed

This is a general outline to follow when writing the code inside the actionPerformed subroutine. There are additional details to consider for implementation.

Step One: Write code to translate a single word to Pig Latin.

Step Two: Modify your code to translate an entire sentence to Pig Latin.

Remember to ...

Extra Credit

For extra credit, create a "Clear" button to clear the input TextField and the output TextArea. When the user presses the button, it should clear all text in the TextField input and TextArea output and allow the user to enter and process another sentence.