package cse1030; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; /** * Abstract base class for simple word puzzles such as scrambled word puzzles * (where the puzzle word is made up of the letters of the original word in * random order). This class provides storage and access to the puzzle word. * Subclasses are responsible for providing storage and access to the * solution word or words. * * @author EECS1030_2014_15W * */ public abstract class AbstractWordPuzzle { /** * The puzzle word. */ private String puzzle; /** * Constructor that creates an word puzzle whose puzzle word is * the empty string. */ public AbstractWordPuzzle() { this.puzzle = ""; } /** * Constructor that sets the puzzle word. * * @param puzzleWord * the puzzle word */ public AbstractWordPuzzle(String puzzleWord) { this.puzzle = puzzleWord; } /** * Get the solution word. For word puzzles with more than one solution this * method returns the solution that comes first in dictionary order. * * @return the solution word that comes first in dictionary order */ public abstract String getSolution(); /** * Get a sorted set of all of the solution words. AbstractWordPuzzle * provides an implementation of this method that returns the * set containing one string equal to the string returned * by this.getSolution(). Subclasses should override this method * if the word puzzle has multiple solutions. * *

* The set returned by this * method cannot be used to modify the solutions for this puzzle. * * @return a sorted set of solution words */ public SortedSet getSolutions() { TreeSet t = new TreeSet(); t.add(this.getSolution()); return t; } /** * Get the puzzle word * * @return the puzzle word */ public final String getPuzzleWord() { return this.puzzle; } /** * Set the puzzle word for this puzzle. * * @param puzzleWord the puzzle word */ public final void setPuzzleWord(String puzzleWord) { this.puzzle = puzzleWord; } /** * Get the string representation of the puzzle. The string is the puzzle * word, followed by " : " followed by a comma separated * list of solutions words. * * @see java.lang.Object#toString() * * @return the string representation of the puzzle */ public String toString() { StringBuilder s = new StringBuilder(this.getPuzzleWord() + " : "); Iterator i = this.getSolutions().iterator(); while (i.hasNext()) { s.append(i.next()); if (i.hasNext()) { s.append(", "); } } return s.toString(); } }