package code; import java.io.IOException; import java.nio.file.Files; import jav
ID: 3598402 • 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
*
* 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<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 3
*
* 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
}
/* 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;
}
Explanation / Answer
package code;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
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
*
* 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) {
_words = readDictionaryFromFile(filename);
_seedWords = filterWordsForLength(_words, MAXIMUM_WORD_LENGTH);
_wordsToFind = null;
}
private ArrayList<String> filterWordsForLength(ArrayList<String> _words, int maximumWordLength) {
ArrayList<String> words = new ArrayList<String>();
for(String word:_words){
if(word.length()==maximumWordLength){
words.add(word);
}
}
return words;
}
/* QUESTION 2
*
* 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) {
ArrayList<String> words = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
while(br.ready()){
String line = br.readLine();
String [] tokens = line.split("\s+");
for(String token : tokens){
if(token.length()>=MINIMUM_WORD_LENGTH && token.length()<=MAXIMUM_WORD_LENGTH){
words.add(token);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return words;
}
/* QUESTION 3
*
* 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) {
_wordsToFind = new HashMap<String, Boolean>();
for(String word : _words){
if(checkAnagram(seed, word)){
_wordsToFind.put(word, false);
}
}
}
private boolean checkAnagram(String str1, String str2){
int len1 = str1.length();
int len2 = str2.length();
boolean found=false,anagram=true;
if(len1 == len2)
{
for(int i=0; i<len1; i++)
{
found=false;
for(int j=0; j<len1; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found = true;
break;
}
}
if(found == false)
{
anagram=false;
break;
}
}
}else{
anagram = false;
}
return anagram;
}
/* 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) {
if(_wordsToFind.containsKey(guess)){
_wordsToFind.put(guess, true);
return true;
}else{
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.