In this project, you are provided a dictionary file named dict.txt . You need to
ID: 3704128 • Letter: I
Question
In this project, you are provided a dictionary file named dict.txt. You need to create a client program Project1.java that use the dictionary file to find all anagram phases that matches a given word.
Your java program should first read in the dictionary file and sort it, but instead of sorting in alphabetical order it should sort according to each word’s canonical form. The canonical form of a word contains the same letters as the original, but in sorted order.
Thus, the canonical form of “computer” is “cemoprtu”, and the canonical form of “program” is “agmoprr”. When your dictionary file is sorted, the word “program” would be placed before the word “computer”, because its canonical form comes first in alphabetical order.
If the word you entered is not contained in the file, print out a message “The word is not existed in the dictionary”, then prompt user to reenter.
Sample output:
Welcome to Yourfirstname Lastname’s anagram solver.
Please enter the dictionary file you want to use: dict.txt
Word to scramble (Enter N/n to quit)? share
All words found in “share”:
hares
hears
share
shear
Word to scramble (Enter N/n to quit)? tap
All words found in “tap”:
apt
pat
tap
Word to scramble (Enter N/n to quit)? xttrt
The word is not existed in the dictionary
Word to scramble (Enter N/n to quit)? N
Thank you for using Yourfirstname Lastname’s anagram solver!
Have a nice day!
Explanation / Answer
here is your program : ----------->>>>>>>>>
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
public class Project1{
public static String canonical(String s){
StringBuilder sb = new StringBuilder(s);
char temp;
for(int i = 0;i<s.length();i++){
for(int j = i;j<s.length();j++){
if(sb.charAt(i) > sb.charAt(j)){
temp = sb.charAt(i);
sb.setCharAt(i,sb.charAt(j));
sb.setCharAt(j,temp);
}
}
}
return new String(sb);
}
public static void sort(String[] dw,String[] cW){
String temp;
for(int i = 0;i<cW.length;i++){
for(int j = i;j<cW.length;j++){
if(cW[i].compareTo(cW[j]) > 0){
temp = cW[i];
cW[i] = cW[j];
cW[j] = temp;
temp = dw[i];
dw[i] = dw[j];
dw[j] = temp;
}
}
}
}
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>();
System.out.println("Welcome To yourfirstname yourlastname anagram solver");
System.out.println("Please enter the dictionary file you want to use ? ");
Scanner sc = new Scanner(System.in);
String file = sc.next();
try{
Scanner fsc = new Scanner(new File(file));
String line;
while(fsc.hasNext()){
line = fsc.next();
words.add(line);
}
}catch(Exception e){
System.out.println("File Error ");
}
Object[] arr = words.toArray();
String[] dictWord = new String[arr.length];
for(int i = 0;i<arr.length;i++){
dictWord[i] = (String)arr[i];
}
String[] canWord = new String[dictWord.length];
for(int i = 0;i<dictWord.length;i++){
canWord[i] = canonical(dictWord[i]);
}
sort(dictWord,canWord);
String sch = "";
String ang = "";
while(true){
System.out.println("Word To Scramble (Enter N/n tp quit):");
sch = sc.next();
if(sch.equals("n") || sch.equals("N")){
break;
}
ang = canonical(sch);
System.out.println("All Words Found in ""+sch+"" : ");
for(int i = 0;i<canWord.length;i++){
if(ang.equals(canWord[i])){
System.out.println(dictWord[i]);
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.