Declare a private final List<String> named dictionary to store the contents from
ID: 3878833 • Letter: D
Question
Declare a private final List<String> named dictionary to store the contents from Dictionary.txt.
Declare and allocate a private List<String> named list to store the list of valid words contained in the specified String.
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:
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.
A list of search strings for the word "there" is shown below.
WARNING
Implement the toString method using the following format.
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<String> dictionary;
private List<String> list;
private String word;
private int count;
/**
* Constructor for the QueryResults class. <br>
* 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<String> getDictionary() {
return dictionary;
}
/**
* Getter method for the list object.
* @return {@link java.util.List} of words found in {@link QueryResults#searchDictionary} method.
*/
public List<String> getList() {
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<String> readDictionary(String filename) throws FileNotFoundException{
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}.
* <p>
*
* <ol>
* <li> Assign {@code word} into the corresponding member variable
* <li> Set the member variable for count back to 0
* <li> Assign {@code list} to the return value for {@link QueryResults#searchDictionaryHelper(List, String)} method.
* Pass in a new ArrayList and the word as parameters.
* </ol>
*
* @param word the word to query.
*/
public void searchDictionary(String word) {
}
/**
* Helper method for {@code searchDictionary}. <br>
*
* Your recursive logic goes here:
*
* <ol>
* <li> If the word is less than or equal to two characters, return {@code wordList}.
* <li> Increment the counter.
* <li> Add the word if it is in the dictionary but not already in the list.
* <li> Recursively call the method twice: Once with the first character removed
* and once with the last character removed.
* </ol>
* 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<String> searchDictionaryHelper(List<String> wordList, String word){
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);
}
}
WARNING
Don’t forget to remove the print statement after you have verified that the method works correctly.Explanation / Answer
here is your program : ----------------------->>>>>>>>>>>>>>>>>
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<String> dictionary;
private List<String> list;
private String word;
private int count;
/**
* Constructor for the QueryResults class. <br>
* 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
count = 0;
}
/**
* Getter method for the dictionary object.
* @return {@link java.util.List} dictionary object
*/a new ArrayList for the list variable.
dictionary = new ArrayList<>();
list = new ArrayList<>();
word = "";
public List<String> getDictionary() {
return dictionary;
}
/**
* Getter method for the list object.
* @return {@link java.util.List} of words found in {@link QueryResults#searchDictionary} method.
*/
public List<String> getList() {
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<String> readDictionary(String filename) throws FileNotFoundException{
List<String> dict = new ArrayList<>();
File f = New File(filename);
Scanner sc = new Scanner(f);
while(sc.hasNext()){
dict.add(sc.next());
}
return dict;
}
/**
* Recursively search substrings of the parameter {@code word}
* and store uniquely in the {@link java.util.List} {@code list}.
* <p>
*
* <ol>
* <li> Assign {@code word} into the corresponding member variable
* <li> Set the member variable for count back to 0
* <li> Assign {@code list} to the return value for {@link QueryResults#searchDictionaryHelper(List, String)} method.
* Pass in a new ArrayList and the word as parameters.
* </ol>
*
* @param word the word to query.
*/
public void searchDictionary(String word) {
this.word = word;
count = 0;
list = searchDictionaryHelper(new ArrayList<String>(),word);
}
/**
* Helper method for {@code searchDictionary}. <br>
*
* Your recursive logic goes here:
*
* <ol>
* <li> If the word is less than or equal to two characters, return {@code wordList}.
* <li> Increment the counter.
* <li> Add the word if it is in the dictionary but not already in the list.
* <li> Recursively call the method twice: Once with the first character removed
* and once with the last character removed.
* </ol>
* 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<String> searchDictionaryHelper(List<String> wordList, String word){
if(word.length() <= 2){
return wordList;
}
int st = 0;
for(int i = 0;i<wordList.size();i++){
if(word.equals(wordList.get(i))){
st = 1;
}
}
if(st != 1){
for(int i = 0;i<dictionary.size();i++){
if(word.equals(dictionary.get(i))){
wordList.add(word);
break;
}
}
}
wordList = searchDictionaryHelper(wordList,word.substring(1,word.length()));
wordList = searchDictionaryHelper(wordList,word.substring(0,word.length()-1));
count++;
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
String temp = "Dictionary has "+dictionary.size()+" words "+"Original String is ""+word+"" String size ="+word.length()+" Method called "+count+" times contains: ";
for(int i = 0;i<list.size();i++){
temp += list.get(i)+" ";
}
return temp;
}
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.