Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code from the above image import java.io.File; import java.io.FileNotFoundExcept

ID: 3878536 • Letter: C

Question

Code from the above image

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

/**

   * 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.

}

/**

   * Getter method for the dictionary object.

   * @return {@link java.util.List} dictionary object

   */

public List<String> getDictionary() {

return null;

}

/**

   * Getter method for the list object.

   * @return {@link java.util.List} of words found in {@link QueryResults#searchDictionary} method.

   */

public List<String> getList() {

return null;

}

/**

   * Get number of recursive calls.

   * @return the number of recursive calls used in {@code searchDictionary}.

   */

public int getCount() {

return 0;

}

/**

   * Getter method for the current string being queried.

   * @return the word being queried

   */

public String getWord(){

return null;

}

/**

   * 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{

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);

}

}

Example of the dictonary words used, but just a regular dictonary

a

aardvark

aardwolf

ab

aba

abaca

abacist

aback

abactinal

abacus

abaddon

abaft

abalienate

abalienation

abalone

abampere

abandon

abandoned

abandonment

abarticulation

abase

abased

abasement

abash

abashed

abashment

abasia

abasic

abate

abatement

abating

abatis

abatjour

abattis

abattoir

abaxial

abba

5 import 7 public class QueryResults 8 class variables go here 100 / Constructor for the QueryResults class. Initialize the dictionary and list objects eparam filename name of the dictionary file 12 14 ethrows FileNotFoundException exception is thrown to the calling method if unable to open file 16 public QueryResults (String filename) throws FileNotFoundException t 7Call the readDictionary method to assign contents to the member variable, dictionary /Instantiate a new ArrayList for the list variable 20 21 e sp 23 24 25 26 Getter method for the dictionary object. ereturn [elink java.util.List dictionary object public List getDictionaryO return nul 28 29 30 e s Getter method for the list object * @return {@link java .util List} of words found in {@link QueryResults#searchDictionary} method. 32 public List getListO 34 35 36 return nul e sp Get number of recursive calls 39 40 ereturn the number of recursive calls used in tecode searchDictionary. 41 public int getCount return 0

Explanation / Answer


Given below is the code for the question.
Note: MAKE SURE YOU PASS 2 COMMAND LINE ARGUMENTS i.e name of dictionary file and the word to search to the program
e.g. Dictionary.txt there
If command line arguments are not passed, you will get ArrrayindexOutOfBoundsException.
Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i




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 = 0;
/**
* 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.
dictionary = readDictionary(filename);
// Instantiate a new ArrayList for the list variable.
list = new ArrayList<String>();
}


/**
* 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{
Scanner f = new Scanner(new File(filename));
List<String> dic = new ArrayList<String>();
while(f.hasNext())
{
dic.add(f.next());
}
f.close();
return dic;
}

/**
* 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;

System.out.println("SEARCH: " + word);
count++;
if(dictionary.contains(word) && !wordList.contains(word))
wordList.add(word);

searchDictionaryHelper(wordList, word.substring(0, word.length()-1));
searchDictionaryHelper(wordList, word.substring(1));

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 str = "Dictionary has " + dictionary.size() + " words ";
str += "Original string is "" + word + """ + " ";
str += "String size equals " + word.length() + " ";
str += "Method called " + count + " times ";
str += "Contains:" + " ";
for(String s : list)
str += s + " ";

return str;
}

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);

}
}
output (Passing 2 command line arguments: Dictionary.txt aback)
=======
SEARCH: aback
SEARCH: abac
SEARCH: aba
SEARCH: bac
SEARCH: back
SEARCH: bac
SEARCH: ack
Dictionary has 37 words
Original string is "aback"
String size equals 5
Method called 7 times
Contains:
aback
aba

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote