Some objects can be doubled, for example a bank account. The result would be a bank account with twice the balance. Some objects can't be doubled. For example, there is no double of a dime coin—no twenty cent coin exists.

The Doublable interface specifies a method for making an object that is double of the current one. It should only be implemented by classes for which doubling makes sense.

Your job is to modify the Word class of chapter 6 (which is reproduced below, with the bugs fixed) so that it implements the Doublable interface. Doubling a word should repeat its text. For example,

new Word("Java").makeDouble()

should produce a Word object with text "JavaJava".

Complete the following file:

/* 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 { /** 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; }

Use the following files:

public interface Doublable 
   Doublable makeDouble();

public class WordTester
   public static void main(String[] args)
      Word w = new Word("Hello");
      System.out.println("Expected: HelloHelloHelloHello");