-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplurals.java
executable file
·102 lines (85 loc) · 2.81 KB
/
plurals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Bluej project: plurals
public class Word
{
public Word(String letters)
{
this.letters = letters.toLowerCase();
}
/**
Forms the plural of this word.
@return the plural, using the rules for regular nouns
*/
public String getPluralForm()
{
// TODO: Complete this method
// If the word ends in y preceded by a consonant you take away the y and add ies.
// If the word ends in y preceded by a vowel, you just add an s.
// You add an es when a word ends in o, or s, or sh, or ch.
// In all the other case just add an s.
// you can use the
// isVowel
// isConsonant
// is
// methods from below.
String last = letters.substring(letters.length() - 1);
String penultimate_letter = letters.substring(letters.length() - 2);
String last_two = letters.substring(letters.length() - 2,letters.length() - 1);
int index = letters.indexOf(last);
int penultimate_index = letters.indexOf(penultimate_letter);
int index_of_last_two = letters.indexOf(last_two);
if (is(index,"y") && isConsonant(penultimate_index))
{
String last_letter = "ies";
letters.replace(last,last_letter);
}
else if(is(index,"y") && isVowel(penultimate_index))
{
String last_letter = "s";
letters+=last_letter;
}
else if( is(index,"o") || is(index,"s") || (is(index,"h") && isConsonant(penultimate_index)) )
{
String last_letter = "es";
letters+=last_letter;
}
else
{
String last_letter = "s";
letters+=last_letter;
}
return letters;
}
/**
Tests whether the ith letter is a vowel.
@param i the index of the letter to test
@return true if the ith letter is a vowel
*/
public boolean isVowel(int i)
{
return is(i, "a")
|| is(i, "e")
|| is(i, "i")
|| is(i, "o")
|| is(i, "u");
}
/**
Tests whether the ith letter is a consonant.
@param i the index of the letter to test
@return true if the ith letter is a consonant
*/
public boolean isConsonant(int i)
{
return !isVowel(i);
}
/**
* Checks what letter is in position i
* @return true when the the letter of letters is the given letter.
* false otherwise.
* @param i index in letters
* @param letter the letter to match with. must be one character long.
*/
public boolean is(int i, String letter)
{
return letters.substring(i, i + 1).equals(letter);
}
}