Question: How is this done without using ArrayList? Prompt: Add these two methods to WordGame.java: boolean contains(String word, String letters) contains determines whether the word specified
How is this done without using ArrayList?
Prompt:
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.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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)
{
if (word == null || word.trim().length() == 0) {
return false;
}
if (letters == null || letters.trim().length() == 0) {
return false;
}
boolean containsLetters = false;
//clean
word=WordGame.clean(word);
letters = WordGame.clean(letters);
//get a list of alphabets
List
List
//use containsAll() method in List class
containsLetters = letteralphas.containsAll(wordalphas);
return containsLetters;
}
/// 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) {
if (words == null || letters == null) {
return null;
}
ArrayList
//add to list if the word contains letters
for (String w : words) {
if (WordGame.contains(w, letters)) {
list.add(w);
}
}
//find largest among the matched words
int largestString = 0;
String result = "";
for (int i = 0; i < list.size(); i++) {
if (list.get(i).length() > largestString) {
largestString = list.get(i).length();
result = list.get(i);
}
}
//last in the list gets selected in case there are multiple long words with the same length
return result;
}
// ---------------------------------------------------------------------
// 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();
}
}
Here is the algorithm for contains()
//null check
//clean
//get a list of alphabets
//use containsAll() method in List class to check the match
Here is the algorithm for search()
//null check
//add to list if the word contains letters
//find largest among the matched words//use containsAll() method in List class to check the match
//last in the list gets selected in case there are multiple long words with the same length
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
