package code; import java.io.IOException; import java.nio.file.Files; import jav
ID: 3596838 • 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 _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
*
* 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)
*
* 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 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 <= MAXIMUM_WORD_LENGTH.
*
* @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;
}
Explanation / Answer
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class JarUtils {
// Determines the maximum length of a word
private static final int MAXIMUM_WORD_LENGTH = 7;
// Determines the minimum 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
// NOTE: do not create variable with Child Type. i.e by ArrayList. Always use SuperType i.e List
// and use dimond
// bracket to specify it's type.
private List<String> _words;
// Holds all words from the dictionary file that have the max length
private List<String> _seedWords;
// Holds all words from _words that must be found by the player
private Map<String, Boolean> _wordsToFind;
/* QUESTION 1
*
* 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)
*
* 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) {
if (this._words != null) {
for (String word : _words) {
boolean isAnagram = isAnagram(seed, word);
if (isAnagram) {
if (this._wordsToFind == null) {
this._wordsToFind = new HashMap<>();
}
this._wordsToFind.put(word, false);
}
}
}
}
/* 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 <= MAXIMUM_WORD_LENGTH.
*
* @param filename - the name of a file of words (a "dictionary file")
* @return an ArrayList containing words
*/
// NOTE
// do not return ArrayList or any other implementation of interface.
// i.e List is interface while arrayList is Implementation of it.
// Always return Super Type.
public List<String> readDictionaryFromFile(String filename) {
List<String> returnList = new ArrayList<>();
try {
File f = new File(filename);
if (f.exists()) {
Scanner sc = new Scanner(new File(filename));
String singleLine = sc.nextLine();
String[] wordsOfLine = singleLine.split(" +");
if (wordsOfLine != null) {
for (String word : wordsOfLine) {
if (word.length() >= MINIMUM_WORD_LENGTH
&& word.length() <= MAXIMUM_WORD_LENGTH) {
returnList.add(word);
}
}
}
} else {
System.err.println("no file found by filename=" + filename);
}
} catch (Exception e) {
System.err.println("exception while performing readDictionaryFromFile. Exception=" + e);
}
return returnList;
}
// UTILITY to check two string are anagram
public boolean isAnagram(String str1, String str2) {
String s1 = str1.replaceAll("\s", "");
String s2 = str2.replaceAll("\s", "");
boolean status = true;
if (s1.length() != s2.length()) {
status = false;
} else {
char[] ArrayS1 = s1.toLowerCase().toCharArray();
char[] ArrayS2 = s2.toLowerCase().toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
}
return status;
}
}
Let me know if you have any question regarding the solution. And also look at the NOTE part of the Above code which will help you more in Java concepts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.