Question
Add these two methods to WordGame.java: boolean contains(String word, String letters) contains determines whether the word specified by the parameter word can be spelled using
Add these two methods to WordGame.java:
boolean contains(String word, String letters)
contains determines whether the word specified by the parameter word can be spelled using the characters in the parameter letters. The parameter letters contains the set of letters with which to try to spell the word specified by the parameter word. This method returns true if word can be spelled using letters; otherwise, the method returns false. There are some edge cases that must be addressed and these edge cases are clearly enumerated by the unit tests. In general, you need to handle what happens if any of the parameters are null.
String search(String[] words, String letters)
search searches through the dictionary of words provided by the words parameter and identifies the longest word that can be spelled using the characters provided by the letters parameter. search returns null if any of the parameters are invalid references, returns an empty string if no word is found, or returns the word if a word is found. Again, there are a number of edge cases that must be addressed and these edge cases are enumerated by the unit tests and accompanying documentation. In addition to handling null parameters, you must also handle what happens when a second word is found that has the same length as an already identified word.
public class WordGame {
// ---------------------------------------------------------------------
// Base
// ---------------------------------------------------------------------
/// contains determines whether word can be spelled using the characters
/// in letters
/// @param word the word that will try to be spelled
/// @param letters the set of letters with which to try to spell word
/// @return true if word can be spelled using letters; otherwise, false
static boolean contains(String word, String letters)
{
//TODO: FILL IN YOUR CODE HERE
// default assumption.you are welcome to change this as needed
return false;
}
/// search locates the longest word in the set of words that can be
/// spelled using the characters in letters.
/// @param letters the set of letters with which to try to spell word
/// @param words the dictionary of words that is to be searched
/// @return null if any of the parameters are invalid references, an
///empty string if no word in words is found, or a word
///selected from words if a word is found
public static String search(String[] words, String letters) {
//TODO: FILL IN YOUR CODE HERE
// default assumption.you are welcome to change this as needed
return "";
}
// ---------------------------------------------------------------------
// Challenge
// ---------------------------------------------------------------------
/// collect composes a list of all words in the set of words that can be
/// spelled using the characters in letters.
/// @param letters the set of letters with which to try to spell word
/// @param words the dictionary of words that is to be searched
/// @return null if any of the parameters are invalid references, an
///empty array if no word in words is found, or the set of all
///words selected from words that are found that can be spelled
///using letters
static String[] collect(String[] words, String letters) {
//TODO: FILL IN YOUR CODE HERE
return null;
}
// ---------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------
/// This method trims out all characters that are not alphas and then
/// converts the string to lowercase.
/// @param s the string to clean
/// @return the cleaned string
private static String clean(String s)
{
return s.replaceAll("[^a-zA-Z]","").toLowerCase();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started