/** Class representating a word composed from Java chars. * Word := EmptyWord + ConsWord(char, Word) */ abstract class Word { /** returns a WordList containing all arrangements (permutations) of the characters in this Word. */ abstract WordList arrangements(); /** returns a WordList containing a list of all Words that can be formed from this by inserting ch * in some postion (ranging from preceding all characters to following all characters) in this Word. */ abstract WordList insertEverywhere(char ch); /** returns a String representation of this Word as specified in each concrete subclass. */ abstract String wordString(); /** returns this Word with char ch added to the front. */ Word consChar(char ch) { return new ConsWord(ch, this); } } /** Singleton class representing the empty Word. */ class EmptyWord extends Word { /** Single instance of EmptyWord. */ static EmptyWord ONLY = new EmptyWord(); private EmptyWord() { } WordList arrangements() { return null; /* . . . */ } WordList insertEverywhere(char ch) { return null; /* . . . */ } String wordString() { return ""; } } /** Recursive class representing a non-empty word. */ class ConsWord extends Word { char first; Word rest; WordList arrangements() { return null; /* . . . rest.arrangements() . . . */ } WordList insertEverywhere(char ch) { return null; /* . . . rest.insertEverywhere(ch) . . . */ } String wordString() { return first + rest.wordString(); } } /** WordList = EmptyWordList + ConsWordList(Word, WordList) */ abstract class WordList { /** returns a list containing all Words derived from this by inserting ch in some position (ranging from * preceding all characters to following all characters) in some Word of this. */ abstract WordList insertEverywhereInAllWords(char ch); /** returns a list containing each word w of this with the character ch added to the front w. */ abstract WordList consAll(char ch); /** forms the concatenation of other with this (concat || this) */ abstract WordList concat(WordList other); /** returns a String representation of this WordList as specified in each concrete subclass. */ abstract String wordListString(); /** returns s String containing the elements of this, where each element is preceded by ' ' */ abstract String wordListStringHelp(); /** returns this WordList with Word w added to the front. */ WordList consWord(Word w) { return new ConsWordList(w, this); } } /** Singleton class representing the empty WordList. */ class EmptyWordList extends WordList { /** Single instance of EmptyWordList. */ static EmptyWordList ONLY = new EmptyWordList(); private EmptyWordList() { } WordList insertEverywhereInAllWords(char ch) { return null; /* . . . */ } WordList consAll(char ch) { return null; /* . . . */ } WordList concat(WordList other) { return other; } String wordListString() { return "()"; } String wordListStringHelp() { return ""; } } class ConsWordList extends WordList { Word first; WordList rest; WordList insertEverywhereInAllWords(char ch) { return null; /* . . . rest.insertEverywhereInAllWords(ch) . . . */ } WordList consAll(char ch) { return null; /* . . . rest.consAll(ch) . . . */ } WordList concat(WordList other) { return rest.concat(other).consWord(first); } String wordListString() { return "(" + first.wordString() + rest.wordListStringHelp() + ")"; } String wordListStringHelp() { return " " + first.wordString() + rest.wordListStringHelp(); } }