package test4; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; /** * A word puzzle is a special kind of puzzle. Each word puzzle * has a collection of words. The word puzzle and its collection * of words form a composition. * */ public class WordPuzzle extends Puzzle { private Set<String> words; /** * Initializes this word puzzle to be easy and have the given * collection of words. * * @param words the collection of words of this word puzzle. * @pre. words != null */ public WordPuzzle(Set<String> words) { super(); this.setWords(words); } /** * Returns the collection of words of this word puzzle. * * @return the collection of words of this word puzzle. */ public Set<String> getWords() { return new HashSet<String>(this.words); } /** * Sets the collection of words of this word puzzle to * the given collection. * * @param words the new collection of words of this word puzzle. * @pre. words != null */ public void setWords(Set<String> words) { this.words = new HashSet<String>(words); } /** * Returns the number of words of this word puzzle. * * @return the number of words of this word puzzle. */ public int numberOfWords() { return this.getWords().size(); } /** * Returns a string representation of this word puzzle. * For example, an easy word puzzle with the words test, * awesome and question is represented as * "Easy puzzle: awesome question test" * * @return "Easy puzzle: ", "Intermediate puzzle: " or "Difficult puzzle: ", * depend of the level of this puzzle followed by the words of this * word puzzle sorted lexicographically (as in a dictionary). */ public String toString() { StringBuffer representation = new StringBuffer(super.toString()); representation.append(":"); for (String word : new TreeSet<String>(this.getWords())) { representation.append(" "); representation.append(word); } return representation.toString(); } }