Question
Declare a private final List named dictionary to store the contents from Dictionary.txt. Declare and allocate a private List named list to store the list
Declare a private final List
Declare and allocate a private List
Declare a private String named word to store the current word being queried.
Declare and initialize a private int named count to keep track of the number of recursive calls made.
Methods
Implement the constructor for QueryResults.
Implement the following getter methods:
getCount
getDictionary
getList
getWord
Implement the readDictionary method.
At this stage you should test the readDictionary method to make sure that it is correct. Do this by adding the following code to your main method after the statement that instantiates your QueryResults object:
System.out.printf("Entry one: %s Entry two: %s ", qr.getDictionary().get(0), qr.getDictionary().get(1));
Once you are convinced that the readDictionary method works, work on the first line of the toString method. This will look like Dictionary has 69901 words.
Implement searchDictionary and searchDictionaryHelper
After you have finished implementing the methods you can test to make sure you are doing the recursion correctly by printing out each String inside the helper method.
System.out.println("SEARCH: " + word);
A list of search strings for the word "there" is shown below.
SEARCH: there SEARCH: here SEARCH: ere SEARCH: her SEARCH: ther SEARCH: her SEARCH: the
WARNING | Dont forget to remove the print statement after you have verified that the method works correctly. |
Implement the toString method using the following format.
Dictionary has 69901 words Original string is "there" String size equals 5 Method called 7 times Contains: there here ere her the
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QueryResults {
// class variables go here
private final List
private List
private String word;
private int count;
/**
* Constructor for the QueryResults class.
* Initialize the dictionary and list objects
* @param filename name of the dictionary file
* @throws FileNotFoundException exception is thrown to the calling method if unable to open file
*/
public QueryResults(String filename) throws FileNotFoundException {
// Call the readDictionary method to assign contents to the member variable, dictionary.
// Instantiate a new ArrayList for the list variable.
dictionary = new ArrayList<>();
list = new ArrayList<>();
word = "";
count = 0;
}
/**
* Getter method for the dictionary object.
* @return {@link java.util.List} dictionary object
*/
public List
return dictionary;
}
/**
* Getter method for the list object.
* @return {@link java.util.List} of words found in {@link QueryResults#searchDictionary} method.
*/
public List
return list;
}
/**
* Get number of recursive calls.
* @return the number of recursive calls used in {@code searchDictionary}.
*/
public int getCount() {
return count;
}
/**
* Getter method for the current string being queried.
* @return the word being queried
*/
public String getWord(){
return word;
}
/**
* Create a new {@link java.util.List} and then read each token from
* the file called {@code filename} and add it into the
* {@link java.util.List}.
*
* @param filename name of file
* @return A list of legal words to use for this recitation
* @throws FileNotFoundException exception is thrown to the calling method if unable to open file.
* @see Scanner#hasNext()
* @see Scanner#next()
*/
public List
File f = New File(filename);
return null;
}
/**
* Recursively search substrings of the parameter {@code word}
* and store uniquely in the {@link java.util.List} {@code list}.
*
*
*
- Assign {@code word} into the corresponding member variable
*
- Set the member variable for count back to 0
*
- Assign {@code list} to the return value for {@link QueryResults#searchDictionaryHelper(List, String)} method.
* Pass in a new ArrayList and the word as parameters.
*
*
*
* @param word the word to query.
*/
public void searchDictionary(String word) {
}
/**
* Helper method for {@code searchDictionary}.
*
* Your recursive logic goes here:
*
*
- If the word is less than or equal to two characters, return {@code wordList}.
*
- Increment the counter.
*
- Add the word if it is in the dictionary but not already in the list.
*
- Recursively call the method twice: Once with the first character removed
* and once with the last character removed.
*
*
* Use {@link ArrayList#contains(Object)}.
* @param wordList A List of unique legal substrings from the parameter, word
* @param word The word being queried
* @return A List of unique legal substrings from the parameter, word
* @see ArrayList#contains(Object)
*/
public List
return wordList;
}
/**
* toString for you QueryResults class.
* @return String that prints information about the class
*/
@Override
public String toString() {
// Remember to double check your formatting
return "";
}
public static void main(String [] args) throws FileNotFoundException {
// Instantiate object
QueryResults qr = new QueryResults(args[0]);
// Call searchDictionary method with a word
qr.searchDictionary(args[1].toLowerCase());
// Print list of words found
// you could also type: System.out.println(qr.toString());
System.out.println(qr);
}
}
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