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 wordprivate static final int MAXIMUM_WORD_LENGTH = 7; // Determines the maximum lengthof a word private static final int MINIMUM_WORD_LENGTH = 3; // Holdsall words from the dictionary file that have lengths between the max

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

// Holds all words from the dictionary file that have the max length

private ArrayList _seedWords;

// Holds all words from _words that must be found by the player

private HashMap _wordsToFind;

/* 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 containing all the words from that file whose length is

* >= MINIMUM_WORD_LENGTH and

*

* @param filename - the name of a file of words (a "dictionary file")

* @return an ArrayList containing words

*/

public ArrayList readDictionaryFromFile(String filename) {

// 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, assigns it to _wordsToFine, and enters each such

* 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 getWords() {

return _words;

}

public ArrayList getSeedWords() {

return _seedWords;

}

public HashMap getWordsToFind() {

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 called list and

* an int named length and returns a new ArrayList containing

* the members of list that contain exactly length characters.

*

* For example, is dictionary is an ArrayList, then calling

*

* filterWordsForLength(dictionary, 7)

*

* will produce an ArrayList of seven-letter words.

*

*/

public ArrayList filterWordsForLength(ArrayList words, int length) {

ArrayList answer = new 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 consisting of

* 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 that prints as [W, i, l, m, a]

*

*/

public ArrayList string2charList(String word) {

ArrayList list = new 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. The remove method of the ArrayList removes ONE occurrence from the

* list.

*

* Example: Suppose list is the ArrayList that prints as [b, o, o, k, k, e, e, p, e, r]

* 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 working with,

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

ArrayList initial = string2charList(word);

ArrayListcompare = new 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) and returns

* a collection of words (a HashSet) that are anagrams of some subset of the letters in word.

*

* 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 possibleWords(String word, ArrayList dictionary) {

HashSet words = new 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 wordsToFind = m.getWordsToFind();

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

"Model.java 1 package code; 3e import java . io. IOException; 4 import java.nio.file.Files Q 5 import java.nio.file Paths 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.HashSet; 10 public class Model 12 13 // Determines the maximum Length of a word private static final int MAXIMUM WORD LENGTH-7; I/ Determines the maximum Length of a word private static final int MINIMUM WORD LENGTH 3 15 17 18 19 private ArrayList seedWords 24 25 26 // Holds all words from words that must be found by the player private HashMap seedWords 24 25 26 // Holds all words from words that must be found by the player private HashMap

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 Databases Questions!