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

package code; import java.io.IOException; import java.nio.file.Files; import jav

ID: 3596487 • Letter: P

Question

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

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

private ArrayList<String> _seedWords;

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

private HashMap<String,Boolean> _wordsToFind;

/* QUESTION 1

*

* This method reads the words from the file specified by filename and returns

* an ArrayList<String> containing all the words from that file whose length is

* >= MINIMUM_WORD_LENGTH and <= MAXIMUM_WORD_LENGTH.

*

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

* @return an ArrayList<String> containing words

*/

public ArrayList<String> readDictionaryFromFile(String filename) {

// TODO Auto-generated method stub

return null;

}

/* QUESTION 2

*

* Generates the set of words that can the player needs to find, based on the given seed.

* Creates a new HashMap<String,Boolean>, 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

}

Explanation / Answer

Below is your code

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Scanner;

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

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

private ArrayList<String> _seedWords;

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

private HashMap<String, Boolean> _wordsToFind;

/*

* QUESTION 1

*

* This method reads the words from the file specified by filename and

* returns an ArrayList<String> containing all the words from that file

* whose length is >= MINIMUM_WORD_LENGTH and <= MAXIMUM_WORD_LENGTH.

*

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

*

* @return an ArrayList<String> containing words

*/

public ArrayList<String> readDictionaryFromFile(String filename) throws FileNotFoundException {

ArrayList<String> words = new ArrayList<>();

Scanner sc = new Scanner(new File(filename));

String wrd;

while (sc.hasNext()) {

wrd = sc.next();

if (wrd.length() >= MINIMUM_WORD_LENGTH && wrd.length() <= MAXIMUM_WORD_LENGTH) {

words.add(wrd);

}

}

sc.close();

return null;

}

/*

* QUESTION 2

*

* Generates the set of words that can the player needs to find, based on

* the given seed. Creates a new HashMap<String,Boolean>, 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

HashMap<String, Boolean> newMap = new HashMap<>();

Iterator<String> it = _words.iterator();

String wrd;

while (it.hasNext()) {

wrd = it.next();

if (isAnagram(seed, wrd)) {

newMap.put(wrd, false);

}

}

_wordsToFind = newMap;

}

public boolean isAnagram(String firstWord, String secondWord) {

char[] word1 = firstWord.replaceAll("[\s]", "").toCharArray();

char[] word2 = secondWord.replaceAll("[\s]", "").toCharArray();

Arrays.sort(word1);

Arrays.sort(word2);

return Arrays.equals(word1, word2);

}

}