Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TODO: Implement the Doublable interface. A word is doubled by
- repeating it. For example, new Word("Java").makeDouble() should
- return a word containing "JavaJava".
- This is the Word class from ch06/debugger (with the bugs fixed).
- */
- /**
- This class describes words in a document.
- */
- public class Word implements Doublable
- {
- public Doublable makeDouble()
- {
- Word w = new Word(String.format("%s%s", text, text));
- return w;
- }
- /**
- Constructs a word by removing leading and trailing non-
- letter characters, such as punctuation marks.
- @param s the input string
- */
- public Word(String s)
- {
- int i = 0;
- while (i < s.length() && !Character.isLetter(s.charAt(i)))
- i++;
- int j = s.length() - 1;
- while (j > i && !Character.isLetter(s.charAt(j)))
- j--;
- text = s.substring(i, j + 1);
- }
- /**
- Returns the text of the word, after removal of the
- leading and trailing non-letter characters.
- @return the text of the word
- */
- public String toString()
- {
- return text;
- }
- /**
- Counts the syllables in the word.
- @return the syllable count
- */
- public int countSyllables()
- {
- int count = 0;
- int end = text.length() - 1;
- if (end < 0) return 0; // The empty string has no syllables
- // An e at the end of the word doesn't count as a vowel
- char ch = Character.toLowerCase(text.charAt(end));
- if (ch == 'e') end--;
- boolean insideVowelGroup = false;
- for (int i = 0; i <= end; i++)
- {
- ch = Character.toLowerCase(text.charAt(i));
- String vowels = "aeiouy";
- if (vowels.indexOf(ch) >= 0)
- {
- // ch is a vowel
- if (!insideVowelGroup)
- {
- // Start of new vowel group
- count++;
- insideVowelGroup = true;
- }
- }
- else insideVowelGroup = false;
- }
- // Every word has at least one syllable
- if (count == 0)
- count = 1;
- return count;
- }
- private String text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement