package cse1030; import java.util.SortedSet; import java.util.TreeSet; /** * A word puzzle where the letters 'a', 'e', 'i', 'o', and 'u' are missing from * the puzzle word and replaced with an underscore '_'. For example: * *
* MissingVowelsPuzzle p = new MissingVowelsPuzzle("facetious");
*
*
* System.out.println(p);
*
* outputs: * *
* f_c_t___s : facetious
*
*
* A second example where the puzzle has multiple solutions: * *
* MissingVowelsPuzzle p = new MissingVowelsPuzzle("cat", "cot", "cut");
*
*
* System.out.println(p);
*
* outputs: * *
*
* The set returned by this method cannot be used to modify the solutions for
* this puzzle.
*
* @return a sorted set of solution words
*/
@Override
public SortedSetc_t : cat, cot, cut
*
*
*
* @author EECS1030_2014_15W
*
*/
public class MissingVowelsPuzzle extends AbstractWordPuzzle {
private SortedSetsolution
. The puzzle word is the solution word with the
* letters 'a', 'e', 'i', 'o', and 'u' replaced with an underscore '_'.
*
* @param solution
* a solution word
*/
public MissingVowelsPuzzle(String solution) {
super();
this.setPuzzleWord(MissingVowelsPuzzle.computePuzzleWord(solution));
this.solutions = new TreeSetsolution
. All other possible solutions should also be
* provided.
*
* @param solution
* a solution word
* @param otherSolutions
* all other solutions to the puzzle
* @throws IllegalArgumentException
* if a solution in otherSolutions
is incompatible with
* solution
.
*/
public MissingVowelsPuzzle(String solution, String... otherSolutions) {
this(solution);
for (String s : otherSolutions) {
if (this.getSolution().equals(MissingVowelsPuzzle.computePuzzleWord(s))) {
this.solutions.add(s);
} else {
throw new IllegalArgumentException(s + " is not a solution for "
+ this.getPuzzleWord());
}
}
}
/**
* Get the solution word. If there is 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
*/
@Override
public String getSolution() {
return this.solutions.first();
}
/**
* Get a sorted set of all of the solution words.
*
* solution
word. The
* puzzle word is the solution word with the letters 'a', 'e', 'i', 'o', and
* 'u' replaced with an underscore '_'.
*
* @param solution
* a solution word
* @return the puzzle word
*/
private static String computePuzzleWord(String solution) {
return solution.replaceAll("[aeiou]", "_");
}
}