Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Pig Latin
- // Spec: To create the Pig Latin form of an English word
- // the initial consonant sound is transposed to the end of the word and an ay is affixed
- // Ex.: "banana" would yield anana-bay). Read Wikipedia for more information on rules.
- // This is an Object Oriented version that allows a client to build a PigLatin object.
- // Online Translator: http://www.snowcrest.net/donnelly/piglatin.html
- public class PigLatin
- {
- private String piggyVersion;
- /** Default constructor initializes piggyVersion to the empty String
- */
- public PigLatin()
- {
- piggyVersion = "";
- }
- /** @param words the String used to initialize piggyVersion
- /* Constructor that initializes based on words.
- /* In this case, the constructor will initialize by calling the convertExpression() method.
- */
- public PigLatin(String words)
- {
- convertExpression(words);
- }
- /** @param original the original word which will be transformed to Pig Latin as per spec
- /* @return the Pig Latin version of the original String
- /* This method is used to convert each word individually
- /* utilizes String methods .substring() and .charAt to build piggyVersion
- */
- public String convertWord(String original)
- {
- // First check to see if the word begins with a vowel or a 'consonant sound'
- // Follow the rules as stated in the spec to build and return a Pig Latin
- // version of the original word.
- if (original.charAt(0) == 'a' || original.charAt(0) == 'e' || original.charAt(0) == 'i' || original.charAt(0) == 'o' ||
- original.charAt(0) == 'u' || original.charAt(0) == 'A' || original.charAt(0) == 'E' || original.charAt(0) == 'I' ||
- original.charAt(0) == 'O' || original.charAt(0) == 'U')
- {
- /* if it DOES begin with a vowel */
- piggyVersion = original + "-yay";
- }
- else
- {
- /* if it DOESN'T begin with a vowel */
- piggyVersion = original.substring(1) + "-" + original.substring(0, 1) + "ay";
- }
- if (original.charAt(0) == 't' && (original.charAt(1) == 'r' || original.charAt(1) == 'h'))
- {
- /* extension with 2 letters*/
- piggyVersion = original.substring(2) + "-" + original.substring(0, 1) + original.substring(1, 2) + "ay";
- }
- if (original.charAt(0) == 't' && original.charAt(1) == 'h' && original.charAt(2) == 'r')
- {
- /* extension with 3 letters*/
- piggyVersion = original.substring(3) + "-" + original.substring(0, 1) + original.substring(1, 2) + original.substring(2, 3) + "ay";
- }
- // makes all letters lower case
- piggyVersion = piggyVersion.toLowerCase();
- return piggyVersion;
- }
- /** @expression the original expression with spaces included
- /* This method sets the value of piggyVersion
- /* Utilizes the String method .split() to create an array of Strings
- /* Approach: 1) declare and initilize an array of strings using .split(" ")
- /* 2) step thru the array of words and use the helper method convertWord()
- /* to convert each word and add it to piggyVersion... the Pig Latin version
- */
- public void convertExpression(String expression)
- {
- String result[] = expression.split(" ");
- piggyVersion = "";
- for (int i = 0; i < result.length; i++)
- {
- piggyVersion += convertWord(result[i]) + " ";
- piggyVersion += " ";
- }
- }
- /** @return piggyVersion... it is already a String */
- public String toString()
- {
- return piggyVersion;
- }
- }
- /*
- 1) If the word begins with a consonant, that letter is transposed to the end of the word and an ay is affixed.
- (Ex.: "banana" would yield anana-bay)
- Extension to this rule: technically it is a „consonant sound? that is moved, not just the first letter.
- To do this, you would potentially move more than one letter, until a vowel is encountered. Try it!
- (Ex: “truck” would yield uck-tray)
- 2) If the word begins with a vowel (aeiou only), then we will just add "yay" to the end of it.
- More variations exist, but we?ll stick with these rules for this project.
- */
- -----------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // PigLatinRunner
- // Spec: Simple client to test out the PigLatin object code
- public class PigLatinRunner
- {
- public static void main(String[] args)
- {
- System.out.println("trash:");
- PigLatin wd = new PigLatin("trash");
- System.out.println(wd);
- System.out.println("--------------------------------------------------------------------------------");
- System.out.println("We hold these truths to be self evident:");
- wd = new PigLatin("We hold these truths to be self evident");
- System.out.println(wd);
- System.out.println("--------------------------------------------------------------------------------");
- System.out.println("May the force be with you:");
- wd = new PigLatin("May the force be with you");
- System.out.println(wd);
- System.out.println("--------------------------------------------------------------------------------");
- System.out.println("Once upon a time there were three little pigs:");
- wd = new PigLatin("Once upon a time there were three little pigs");
- System.out.println(wd);
- System.out.println("--------------------------------------------------------------------------------");
- }
- }
- /*
- trash:
- ash-tray
- --------------------------------------------------------------------------------
- We hold these truths to be self evident:
- e-way old-hay ese-thay uths-tray o-tay e-bay elf-say evident-yay
- --------------------------------------------------------------------------------
- May the force be with you:
- ay-may e-thay orce-fay e-bay ith-way ou-yay
- --------------------------------------------------------------------------------
- Once upon a time there were three little pigs:
- once-yay upon-yay a-yay ime-tay ere-thay ere-way ee-thray ittle-lay igs-
- pay
- --------------------------------------------------------------------------------
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement