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

There is no starter file. Use the same inputs you used for the Jumbles Project a

ID: 3823085 • Letter: T

Question

There is no starter file. Use the same inputs you used for the Jumbles Project and the expeced output is identical

Lab#9 is a re-make of the Jumbles project.

Since you know what a TreeSet is you should use it to store your dictionary and jumbles files. No need to call Collections.sort() after the loads.

Your answer key data strucure however must be a HashMap<String,String> or TreeSet<String,String>.

Performance for a TreeMap vs. HashMap will be almost identical for inputs of the size we are using.

Actual runtime speed will be very fast. Should be at most around one second even on an older slower laptop.

Output for Jumbles Project:

Original Jumbles Project:

import java.io.*;
import java.util.*;

public class Project8 {
  
//List for Collecting permutations of word
static HashSet<String> perms;
  
//This method calculates the permutations of the string using recursion and stores in the above list.
static void genPerms(char str[], int ind){
if(ind==str.length - 1){
perms.add(String.valueOf(str));
return;
}
for(int i=ind;i<str.length;i++){
char ch = str[i];
str[i] = str[ind];
str[ind] = ch;
genPerms(str, ind+1);
ch = str[i];
str[i] = str[ind];
str[ind] = ch;
}
}
public static void main(String[] args) throws IOException {
//Reading the dictionary into a set
String dict = args[0];
BufferedReader f1 = new BufferedReader(new FileReader(new File(dict)));
String word;
HashSet<String> hs = new HashSet<>();
while((word = f1.readLine())!=null){
hs.add(word);
}   
  
//Reading the words
String jumbles = args[1];
BufferedReader f2 = new BufferedReader(new FileReader(new File(jumbles)));
  
//List for storing final answer
ArrayList<String> fans = new ArrayList<>();
  
//Iterating through all the jumble words
while((word = f2.readLine())!=null){
//finding permutations
perms = new HashSet<>();
genPerms(word.toCharArray(),0);
String ans = word;
//Storing perms in a List
ArrayList<String> hor = new ArrayList<>();
for(String x : perms){
if(hs.contains(x)){
hor.add(x);
}
}
//Sorting the list
Collections.sort(hor);
//Calculating the answer
for(String x : hor){
ans = ans+" "+x;
}
fans.add(ans);
}
  
//Sorting the answer vertically
Collections.sort(fans);
for(String x : fans){
System.out.println(x);
}
}
}

Explanation / Answer

import java.io.*;
import java.util.*;

public class Main {
  
//List for Collecting permutations of word
static ArrayList<String> perms;
  
//This method calculates the permutations of the string using recursion and stores in the above list.
static void genPerms(char str[], int ind){
if(ind==str.length-1){
perms.add(String.valueOf(str));
return;
}
for(int i=ind;i<str.length;i++){
char ch = str[i];
str[i] = str[ind];
str[ind] = ch;
genPerms(str, ind+1);
ch = str[i];
str[i] = str[ind];
str[ind] = ch;
}
}
public static void main(String[] args) throws IOException {
//Reading the dictionary into a set
String dict = args[0];
BufferedReader f1 = new BufferedReader(new FileReader(new File(dict)));
String word;
HashSet<String> hs = new HashSet();
while((word = f1.readLine())!=null){
hs.add(word);
}   
  
//Reading the words
String jumbles = args[1];
BufferedReader f2 = new BufferedReader(new FileReader(new File(jumbles)));
  
//List for storing final answer
ArrayList<String> fans = new ArrayList();
  
//Iterating through all the jumble words
while((word = f2.readLine())!=null){
//finding permutations
perms = new ArrayList();
genPerms(word.toCharArray(),0);
String ans = word;
//Storing perms in a List
ArrayList<String> hor = new ArrayList();
for(String x : perms){
if(hs.contains(x)){
hor.add(x);
}
}
//Sorting the list
Collections.sort(hor);
//Calculating the answer
for(String x : hor){
ans = ans+" "+x;
}
fans.add(ans);
}
  
//Sorting the answer vertically
Collections.sort(fans);
for(String x : fans){
System.out.println(x);
}
}
}


OUTPUT :
P:>java Main P:/dict.txt P:/jumbles.txt
arpt part tarp trap
atc act cat tac
atr art rat tar
gdo dog god
grof frog
otsp opts post pots spot stop tops
sylogs glossy glossy

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote