Advertisement
Crenox

Pig Latin Java Program

Sep 24th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. // Sammy Samkough
  2. // Pig Latin
  3. // Spec: To create the Pig Latin form of an English word
  4. // the initial consonant sound is transposed to the end of the word and an ay is affixed
  5. // Ex.: "banana" would yield anana-bay). Read Wikipedia for more information on rules.
  6. // This is an Object Oriented version that allows a client to build a PigLatin object.
  7. // Online Translator: http://www.snowcrest.net/donnelly/piglatin.html
  8.  
  9. public class PigLatin
  10. {
  11. private String piggyVersion;
  12.  
  13. /** Default constructor initializes piggyVersion to the empty String
  14. */
  15. public PigLatin()
  16. {
  17. piggyVersion = "";
  18. }
  19.  
  20. /** @param words the String used to initialize piggyVersion
  21. /* Constructor that initializes based on words.
  22. /* In this case, the constructor will initialize by calling the convertExpression() method.
  23. */
  24. public PigLatin(String words)
  25. {
  26. convertExpression(words);
  27. }
  28.  
  29. /** @param original the original word which will be transformed to Pig Latin as per spec
  30. /* @return the Pig Latin version of the original String
  31. /* This method is used to convert each word individually
  32. /* utilizes String methods .substring() and .charAt to build piggyVersion
  33. */
  34. public String convertWord(String original)
  35. {
  36. // First check to see if the word begins with a vowel or a 'consonant sound'
  37. // Follow the rules as stated in the spec to build and return a Pig Latin
  38. // version of the original word.
  39. if (original.charAt(0) == 'a' || original.charAt(0) == 'e' || original.charAt(0) == 'i' || original.charAt(0) == 'o' ||
  40. original.charAt(0) == 'u' || original.charAt(0) == 'A' || original.charAt(0) == 'E' || original.charAt(0) == 'I' ||
  41. original.charAt(0) == 'O' || original.charAt(0) == 'U')
  42. {
  43. /* if it DOES begin with a vowel */
  44. piggyVersion = original + "-yay";
  45. }
  46. else
  47. {
  48. /* if it DOESN'T begin with a vowel */
  49. piggyVersion = original.substring(1) + "-" + original.substring(0, 1) + "ay";
  50. }
  51.  
  52. if (original.charAt(0) == 't' && (original.charAt(1) == 'r' || original.charAt(1) == 'h'))
  53. {
  54. /* extension with 2 letters*/
  55. piggyVersion = original.substring(2) + "-" + original.substring(0, 1) + original.substring(1, 2) + "ay";
  56. }
  57.  
  58. if (original.charAt(0) == 't' && original.charAt(1) == 'h' && original.charAt(2) == 'r')
  59. {
  60. /* extension with 3 letters*/
  61. piggyVersion = original.substring(3) + "-" + original.substring(0, 1) + original.substring(1, 2) + original.substring(2, 3) + "ay";
  62. }
  63.  
  64. // makes all letters lower case
  65. piggyVersion = piggyVersion.toLowerCase();
  66.  
  67. return piggyVersion;
  68. }
  69.  
  70. /** @expression the original expression with spaces included
  71. /* This method sets the value of piggyVersion
  72. /* Utilizes the String method .split() to create an array of Strings
  73. /* Approach: 1) declare and initilize an array of strings using .split(" ")
  74. /* 2) step thru the array of words and use the helper method convertWord()
  75. /* to convert each word and add it to piggyVersion... the Pig Latin version
  76. */
  77. public void convertExpression(String expression)
  78. {
  79. String result[] = expression.split(" ");
  80. piggyVersion = "";
  81.  
  82. for (int i = 0; i < result.length; i++)
  83. {
  84. piggyVersion += convertWord(result[i]) + " ";
  85. piggyVersion += " ";
  86. }
  87. }
  88.  
  89. /** @return piggyVersion... it is already a String */
  90. public String toString()
  91. {
  92. return piggyVersion;
  93. }
  94. }
  95. /*
  96. 1) If the word begins with a consonant, that letter is transposed to the end of the word and an ay is affixed.
  97. (Ex.: "banana" would yield anana-bay)
  98. Extension to this rule: technically it is a „consonant sound? that is moved, not just the first letter.
  99. To do this, you would potentially move more than one letter, until a vowel is encountered. Try it!
  100. (Ex: “truck” would yield uck-tray)
  101. 2) If the word begins with a vowel (aeiou only), then we will just add "yay" to the end of it.
  102. More variations exist, but we?ll stick with these rules for this project.
  103. */
  104. -----------------------------------------------------------------------------------------------------------------------------
  105. // Sammy Samkough
  106. // PigLatinRunner
  107. // Spec: Simple client to test out the PigLatin object code
  108.  
  109. public class PigLatinRunner
  110. {
  111. public static void main(String[] args)
  112. {
  113. System.out.println("trash:");
  114. PigLatin wd = new PigLatin("trash");
  115. System.out.println(wd);
  116. System.out.println("--------------------------------------------------------------------------------");
  117.  
  118. System.out.println("We hold these truths to be self evident:");
  119. wd = new PigLatin("We hold these truths to be self evident");
  120. System.out.println(wd);
  121. System.out.println("--------------------------------------------------------------------------------");
  122.  
  123. System.out.println("May the force be with you:");
  124. wd = new PigLatin("May the force be with you");
  125. System.out.println(wd);
  126. System.out.println("--------------------------------------------------------------------------------");
  127.  
  128. System.out.println("Once upon a time there were three little pigs:");
  129. wd = new PigLatin("Once upon a time there were three little pigs");
  130. System.out.println(wd);
  131. System.out.println("--------------------------------------------------------------------------------");
  132. }
  133. }
  134. /*
  135. trash:
  136. ash-tray
  137. --------------------------------------------------------------------------------
  138.  
  139. We hold these truths to be self evident:
  140. e-way old-hay ese-thay uths-tray o-tay e-bay elf-say evident-yay
  141. --------------------------------------------------------------------------------
  142.  
  143. May the force be with you:
  144. ay-may e-thay orce-fay e-bay ith-way ou-yay
  145. --------------------------------------------------------------------------------
  146.  
  147. Once upon a time there were three little pigs:
  148. once-yay upon-yay a-yay ime-tay ere-thay ere-way ee-thray ittle-lay igs-
  149. pay
  150. --------------------------------------------------------------------------------
  151.  
  152. Press any key to continue . . .
  153. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement