Question: Copy and Paste version same as above Model.java below package code; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; public class






Copy and Paste version same as above
Model.java below
package code;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Model {
// Determines the maximum length of a word
private static final int MAXIMUM_WORD_LENGTH = 7;
// Determines the maximum length of a word
private static final int MINIMUM_WORD_LENGTH = 3;
// Holds all words from the dictionary file that have lengths between the max and min, inclusive
private ArrayList
// Holds all words from the dictionary file that have the max length
private ArrayList
// Holds all words from _words that must be found by the player
private HashMap
/* QUESTION 1
*
* The constructor
*
* The job of the constructor is to assign sensible initial values to each instance variable.
* To _words it should assign the value returned by readDictionaryFromFile, with the filename passed in as argument
* To _seedWords it should assign the value returned by filterWordsForLength, with _words and the maximum word length passed in as arguments
* To _wordsToFind it should assign the value null.
*
* @param filename - the name of a file of words (a "dictionary file")
*/
public Model(String filename) {
// TODO Auto-generated method stub
}
/* QUESTION 2
*
* This method reads the words from the file specified by filename and returns
* an ArrayList
* >= MINIMUM_WORD_LENGTH and
*
* @param filename - the name of a file of words (a "dictionary file")
* @return an ArrayList
*/
public ArrayList
// TODO Auto-generated method stub
return null;
}
/* QUESTION 3
*
* Generates the set of words that can the player needs to find, based on the given seed.
* Creates a new HashMap
* word into the map, paired with the boolean value false (since none of these words have
* yet been found by the player).
*
* HINT: Play the game: https://www.mindgames.com/game/TextTwist+2
*
* The words the player has to find are the words from the dictionary that are anagrams of
* the seed word (which is one the the maximum length words). You wrote a method in part 1
* of HW2 which does most of this :-)
*
* @param seed - the word whose letters make up the inventory of available letters in the game
*/
public void generateWordsToFind(String seed) {
// TODO Auto-generated method stub
}
/* QUESTION 4
*
* Checks whether the guess is a one of the words to be found. If so, updates that word's entry
* in _wordsToFind so it is paired with true rather than false; the method also returns true in
* this case. If not _wordsToFind is not updated and the method returns false.
*
* @param guess - the String being checked to see whether it is one of the words to be found
* @return true if guess is a word to be found, false otherwise
*/
public boolean checkGuess(String guess) {
// TODO Auto-generated method stub
return false;
}
/**
* Accessor methods provided for testing purposes
*
*/
public ArrayList
return _words;
}
public ArrayList
return _seedWords;
}
public HashMap
return _wordsToFind;
}
/** PART 1 **/
/*
* The game will use a dictionary of words.
*
* The 'starter words' are all supposed to be of a certain length.
* In the TextTwist2 game the 'starter words' are all of length 7.
*
* Write a method that takes in an ArrayList
* an int named length and returns a new ArrayList
* the members of list that contain exactly length characters.
*
* For example, is dictionary is an ArrayList
*
* filterWordsForLength(dictionary, 7)
*
* will produce an ArrayList
*
*/
public ArrayList
ArrayList
for(int i = 0; i
if(words.get(i).length()==length) {
answer.add(words.get(i));
}
}
return answer;
}
/*
* This method accepts a String as input and returns an ArrayList
* the characters from the String word.
*
* N.B. Character is a new type for us. A Character represents a single character from a String.
*
* Note that word.charAt(i) returns a value that can be directly added to an ArrayList
* using the add method of the ArrayList
*
* For example, string2charList("Wilma") must yield the ArrayList
*
*/
public ArrayList
ArrayList
for(int i = 0; i
list.add(word.charAt(i));
}
return list;
}
/*
* This method determines whether or not a given String is an anagram of some subset of the
* letters in the ArrayList
*
* See:
* http://www.dictionary.com/browse/anagram
*
* The basic idea here is that we'll loop through each character in word, and remove each word from
* the ArrayList
* list.
*
* Example: Suppose list is the ArrayList
* then list.remove('e') changes list to [b, o, o, k, k, e, p, e, r].
* Calling list.remove('e') again changes list to [b, o, o, k, k, p, e, r].
*
* The remove method returns a boolean value. If the call changes the contents of the ArrayList the
* method returns true. If calling the method does not change the ArrayList then the method
* returns false.
*
* HINT: because this method will remove characters from ArrayList
* it is important to make a copy of what's in reference before using it. Write a loop that copies
* the contents of reference to a new ArrayList
*
*/
public boolean anagramOfLetterSubset(String word, ArrayList
ArrayList
ArrayList
for (int i=0; i compare.add(reference.get(i)); } for(int i = 0; i if(!compare.remove(initial.get(i))) { return false; } } return true; // change the value returned } /* * This method takes a word (a String) and a dictionary of words (an ArrayList * a collection of words (a HashSet * * Put another way, this method finds all the words or length at least 2 that can be played from the * letters in word. * * HashSet is a collection that, for our purposes in this homework, behaves like an ArrayList with * the following exception: * calling add(X) on a HashSet adds X only if X is not already in the collection * In other words, HashSet does not allow duplicate entries. Because HashSet does not allow duplicates * we get unique words in the result. * * HINT: in defining this method you should find a natural use for both string2charList and also * anagramOfLetterSubset. */ public HashSet HashSet for(int i = 0; i if(anagramOfLetterSubset(dictionary.get(i),string2charList(word))){ /*if(words.remove(dictionary.get(i))) { words.add(dictionary.get(i)); }*/ words.add(dictionary.get(i)); } } return words; } } Testing.java below package code; import java.util.HashMap; public class Testing { public static void main(String[] args) { // Instantiate the Models class to access its methods Model m = new Model("dictionaries/small.txt"); System.out.println("All words of proper length: " + m.getWords()); System.out.println(" All words of max length: " + m.getSeedWords()); String seed = "sputnik"; m.generateWordsToFind(seed); HashMap System.out.println("All words to find from 'sputnik' (before guesses): "+wordsToFind); System.out.println("checking guess 'puns' yields "+m.checkGuess("puns")); System.out.println("checking guess 'pun' yields "+m.checkGuess("pun")); System.out.println("checking guess 'spun' yields "+m.checkGuess("spun")); System.out.println("checking guess 'pins' yields "+m.checkGuess("pins")); System.out.println("checking guess 'pin' yields "+m.checkGuess("pin")); System.out.println("checking guess 'spin' yields "+m.checkGuess("spin")); System.out.println("checking guess 'antidisestablishmentarianism' yields "+m.checkGuess("antidisestablishmentarianism")); System.out.println("All words to find from 'sputnik' (after guesses): "+wordsToFind); // Add calls to the other methods similar to the above method call to test your code } } Thanks for your help in advance! I hope to get the correct answers, please! The questions are numbered 1 to 4 with comments on the Model.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
