I have to only write code for the getUnScrambling Method, I don\'t know what to
ID: 3859324 • Letter: I
Question
I have to only write code for the getUnScrambling Method, I don't know what to do? package eecs1022.jumbleapp; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; /** * The dictionary, which is the model of this app. * * @author Franck van Breugel */ public class Dictionary { /** * Initializes this dictionary from the given file. Each line of the file contains a * single word. * * @param file file containing the words of this dictionary. */ public Dictionary(File file) { try { Scanner input = new Scanner(file); while (input.hasNextLine()) { String word = input.nextLine(); } input.close(); } catch (FileNotFoundException e) { // do nothing } } /** * Returns the list of words that are unscramblings of the given word. * * @param word word to be unscrambled. * @return list of words that are unscramblings of the given word. */ public List<String> getUnscramblings(String word) { } }
Explanation / Answer
I have understood your question as follows: -
You have a file containing all the words. Those words you need to save in a dictionary using a hashmap. Then given a word , you need to get all the permutations of that word and check if that word is present in dictionary. If yes, you need to add it to the list which will be returned by getUnscramblings function.
Let me know if I am missing anything in the comments.
below is your code: -
Dictionary.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* The dictionary, which is the model of this app.
*
* @author Franck van Breugel
*/
public class Dictionary {
/**
* Initializes this dictionary from the given file. Each line of the file
* contains a single word.
*
* @param file
* file containing the words of this dictionary.
*/
private HashMap<Integer, String> dictionaryList;
public Dictionary(File file) {
dictionaryList = new HashMap<>();
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine();
dictionaryList.put(word.hashCode(), word);
}
input.close();
} catch (FileNotFoundException e) {
// do nothing
}
}
/**
* Returns the list of words that are unscramblings of the given word.
*
* @param word
* word to be unscrambled.
* @return list of words that are unscramblings of the given word.
*/
public List<String> getUnscramblings(String word) {
List<String> strList = new ArrayList<>();
Set<String> anagrams = permutationFinder(word);
for (String wr : anagrams) {
if (this.dictionaryList.containsValue(wr)) {
strList.add(wr);
}
}
return strList;
}
/**
* Returns the list of words that are anagrams of the given word.
*
* @param word
* word to get anagram.
* @return list of words that are anagrams of the given word.
*/
public static Set<String> permutationFinder(String str) {
Set<String> set = new HashSet<String>();
if (str == "")
return set;
Character a = str.charAt(0);
if (str.length() > 1) {
str = str.substring(1);
Set<String> permSet = permutationFinder(str);
for (String x : permSet) {
for (int i = 0; i <= x.length(); i++) {
set.add(x.substring(0, i) + a + x.substring(i));
}
}
} else {
set.add(a + "");
}
return set;
}
}
DictionaryTester.java
import java.io.File;
import java.util.List;
public class DictionaryTester {
public static void main(String[] args) {
Dictionary dic = new Dictionary(new File("words.txt"));
List<String> unscrabledList = dic.getUnscramblings("list");
System.out.println("Unscrambled list of word "list" are: -");
for(String w: unscrabledList) {
System.out.println(w);
}
}
}
words.txt
word
rdow
list
sitl
ajay
jay
aya
love
ove
Sample Run:-
Unscrambled list of word "list" are: -
list
sitl