Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // Bluej project: lesson5-2/plurals
  2.  
  3. public class Word
  4. {
  5. private String letters;
  6.  
  7. public Word(String letters)
  8. {
  9. this.letters = letters.toLowerCase();
  10. }
  11.  
  12. /**
  13. Forms the plural of this word.
  14. @return the plural, using the rules for regular nouns
  15. */
  16.  
  17.  
  18. public String getPluralForm()
  19. {
  20. // TODO: Complete this method
  21. // If the word ends in y preceded by a consonant you take away the y and add ies.
  22. // If the word ends in y preceded by a vowel, you just add an s.
  23. // You add an es when a word ends in o, or s, or sh, or ch.
  24. // In all the other case just add an s.
  25. // you can use the
  26. // isVowel
  27. // isConsonant
  28. // is
  29. // methods from below.
  30. int lastIndex = letters.length() - 1;
  31.  
  32. if (letters.endsWith("y") && letters.isConsonant(lastIndex - 1)){
  33. return (letters.substring(0, lastIndex)).concat("ies");
  34. }
  35. }
  36.  
  37. /**
  38. Tests whether the ith letter is a vowel.
  39. @param i the index of the letter to test
  40. @return true if the ith letter is a vowel
  41. */
  42. public boolean isVowel(int i)
  43. {
  44. return is(i, "a")
  45. || is(i, "e")
  46. || is(i, "i")
  47. || is(i, "o")
  48. || is(i, "u");
  49. }
  50.  
  51. /**
  52. Tests whether the ith letter is a consonant.
  53. @param i the index of the letter to test
  54. @return true if the ith letter is a consonant
  55. */
  56. public boolean isConsonant(int i)
  57. {
  58. return !isVowel(i);
  59. }
  60.  
  61. /**
  62. * Checks what letter is in position i
  63. * @return true when the the letter of letters is the given letter.
  64. * false otherwise.
  65. * @param i index in letters
  66. * @param letter the letter to match with. must be one character long.
  67. */
  68. public boolean is(int i, String letter)
  69. {
  70. return letters.substring(i, i + 1).equals(letter);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement