Question: Add this method to WordGame.java: boolean contains(String word, String letters) contains determines whether the word specified by the parameter word can be spelled using the

Add this method 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.

import java.io.*;

import java.util.*;

public class WordTool {

public static String[] getDictionary ()

{

return getDictionary ("words");

}

public static String[] getDictionary (String fileName)

{

String[] words = readDictionary (fileName);

String[] scrubbedWords = scrub (words);

return scrubbedWords;

}

static String[] readDictionary (String fileName)

{

String[] words = null;

try {

// Since we don't know in advance how many words there

// are, we'll use a list instead of an array.

LinkedList stringList = new LinkedList();

// Scanner knows how to skip whitespace.

Scanner scanner = new Scanner (new FileReader (fileName));

while (scanner.hasNext()) {

// At each step, get the next word and place in list.

String s = scanner.next();

stringList.addLast (s);

}

// Now that we know the size, we'll make an array.

words = new String [stringList.size()];

Iterator iter = stringList.iterator();

int i = 0;

while (iter.hasNext()) {

words[i] = iter.next();

i ++;

}

// Done.

return words;

}

catch (IOException e) {

System.out.println (e);

System.exit (0);

return null;

}

}

static String[] scrub (String[] words)

{

// Remove words with caps, and single-letter words

int badWords = 0;

for (int i=0; i

if (words[i].length() <= 1) {

badWords ++;

words[i] = null;

}

else if ( Character.isUpperCase (words[i].charAt(0)) ) {

badWords ++;

words[i] = null;

}

}

// Make space for the good words.

String[] realWords = new String [words.length - badWords];

int j = 0;

for (int i=0; i

while (words[j] == null) {

j ++;

}

realWords[i] = words[j];

j ++;

}

return realWords;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!