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.
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.
Your applet should look and function like this:
You need to declare, initialize, and add the following fields and buttons to your applet:
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.
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.
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!
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.
For each individual word you should:
My dog has fleas again
The output in Pig Latin should read
My ogday ashay easflay againway
You should come up with and test more sentences on your own.
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.