Question

In: Computer Science

For this project, you are going to write a program to find the anagrams in a...

For this project, you are going to write a program to find the anagrams in a given dictionary for a given word. If two words have the same letters but in different order, they are called anagrams. For example, “listen” and “silent” are a pair of anagrams.

**JAVA**

First let’s focus on “LetterInventory.java”.

Its purpose is to compute the canonical “sorted letter form” for a given word.

1. Implement the constructor, which takes the String input word. You can assume that the input word contains only valid letters (e.g. no punctuation or blanks), but it may contain letters in different cases.

2. Implement the method: public String getCanonical() It should return the canonical “sorted letter form” for the input word (given in the constructor). It should use only lower cases. For example, if the given word is “AliBaba”, then this method should return “aaabbil”.

****I have already completed all of the above tasks I am struggling with the next part****

**below is the code I have for LetterInventory.java**

import java.util.*;

public class LetterInventory {

   private String input;
  
   //stores the original word.
  
   public LetterInventory(String input) {
       this.input = input;
   }
   //uses an array of chars to hold word and sorts it into alphabetical order.
   //displays it in lower case.
public String getCanonical() {
   input = input.toLowerCase(); // converting input to lower case
   char [] word = input.toCharArray();
   Arrays.sort(word);
   String sorted = new String (word);
   return sorted;
  
}
}

*** this is where i am stuck ***

Once “LetterInventory.java” is working as expected, we can now focus on “AnagramFinder.java”. It will read an input “dictionary” file, which contains quite many words, to build a map. The “key” for the map is a word’s canonical form, the “value” for the map will be all the words in the given dictionary that share the same canonical form. For example, if both “listen” and “silent” are in the dictionary, one entry in the map would have key as “eilnst” and value as an ArrayList of words “listen” and “silent”.

Implement the constructor, which takes in the input dictionary file and build the map. You can assume the words in the dictionary contain only valid letters. You should take advantage of “LetterInventory”.

3. Now implement the method: public void printAnagrams(String word) It should try to search the anagrams for the given word in the map. Again, you can assume the input word contains only valid letters, and you should take advantage of “LetterInventory”.

Print out any anagrams found. For now, don’t worry about the case where the input word is the same as the word inside the dictionary, just print them out.

Here is some sample print out:

Your input is: silent. Found anagrams: listen, silent, tinsel.

Your input is: AliBaba. Sorry, didn’t find any anagrams.

A sample main method is already written for you to test various functionalities with some sample input words.

*** Below is the shell for the main method ***

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

public class AnagramFinder {
public static void main(String[] args) throws FileNotFoundException {
String[] originals = new String[] {
"realfun",
"mias",
"EVIL",
"unable",
"Silent",
"AliBaba"
};

for(String original: originals) {
LetterInventory inv = new LetterInventory(original);
System.out.println("Original: " + original + ", Canonical: " + inv.getCanonical());
}

System.out.println("\n");

File f = new File("HW_sample_dict.txt");

AnagramFinder finder = new AnagramFinder(f);

for(String original: originals) {
finder.printAnagrams(original);
}
}

public AnagramFinder(File f) throws FileNotFoundException {
//TODO implement  
}

public void printAnagrams(String word) {
//TODO implement
}

public void printAnagrams2(String word) {
// Extra Credit: optional to implement
}
}

** can leave out extra credit part i just need help with the first 2 methods in the main file **

** the dict.txt is 400 pages long so I did not include it (the file just contains thousands of words) **

Solutions

Expert Solution

import java.util.*;
import java.io.*;
class LetterInventory {

private String input;
  
//stores the original word.
  
public LetterInventory(String input) {
this.input = input;
}
//uses an array of chars to hold word and sorts it into alphabetical order.
//displays it in lower case.
public String getCanonical() {
input = input.toLowerCase(); // converting input to lower case
char [] word = input.toCharArray();
Arrays.sort(word);
String sorted = new String (word);
return sorted;
  
}
}

public class AnagramFinder {
public static void main(String[] args) throws FileNotFoundException {
String[] originals = new String[] {
"realfun",
"mias",
"EVIL",
"unable",
"Silent",
"AliBaba"
};

for(String original: originals) {
LetterInventory inv = new LetterInventory(original);
System.out.println("Original: " + original + ", Canonical: " + inv.getCanonical());
}

System.out.println("\n");

File f = new File("dict.txt");

AnagramFinder finder = new AnagramFinder(f);

for(String original: originals) {
finder.printAnagrams(original);
}
}
ArrayList<String> b=new ArrayList<String>(); //to store words of dict.txt
public AnagramFinder(File f) throws FileNotFoundException {
//TODO implement
Scanner sc=new Scanner(f);
while (sc.hasNextLine())
   b.add(sc.nextLine()); //add all words to b array

}
public void printAnagrams(String word) {
//TODO implement
Map map=new HashMap(); //create a map
for(int i=0;i<b.size();i++)
{
   ArrayList<String> arr=new ArrayList<String>(); //to store anagrams of a given word.
   if(!(arr.contains(b.get(i))))
   {
       for(int j=0;j<b.size();j++)
       {
           if((b.get(i)).equals(b.get(j))==false)
           {
               char[] m=(b.get(i)).toCharArray();
               char[] n=(b.get(j)).toCharArray();
               Arrays.sort(m);
               Arrays.sort(n);
               int c=0;
               if(m.length==n.length)
               {
               for(int p=0;p<m.length;p++)
               {
                   if(m[p]==n[p])
                       c++;
               }
               }
               if(c==m.length)
               {
                   arr.add(b.get(j));
               }
           }
       }
       arr.add(b.get(i));
       LetterInventory obj = new LetterInventory(b.get(i));
       String a=obj.getCanonical();
       map.put(a,arr);
}
}
LetterInventory ob = new LetterInventory(word);
String e=ob.getCanonical();
if(map.get(e)==null)
   System.out.println("Your input is:"+word+" "+"Sorry, did'nt find any anagrams.");
else
{
   System.out.println("Your input is:"+word+" "+"Found anagrams:"+map.get(e));
}
}

public void printAnagrams2(String word) {
// Extra Credit: optional to implement
}
}

Screenshots:


Related Solutions

PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter a word or empty string to finish: poster The word poster has the following 6 anagrams: presto repost respot stoper topers tropes
3. Write a Java program that discover all anagrams of all wordslisted in the input file,...
3. Write a Java program that discover all anagrams of all wordslisted in the input file, “dict.txt”. An anagram of a work is a rearrangement of its letters into a new legal word. Your program should do the following: a. Read in the given “dict.txt” file and sort it in each word’s canonical form. The canonical form of a word contains the same letters as the original word, but in sorted order b. Instead of putting the “dict.txt” in the...
In this assignment you will be implementing a function that will find all single-word anagrams given...
In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter....
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
in paython You are going to make an advanced ATM program When you run the program,...
in paython You are going to make an advanced ATM program When you run the program, it will ask you if you want to: Either log in or make a new user Exit Once I have logged in, I will have 3 balances, credit, checking, and savings. From here it will ask which account to use. Once the account is picked they can deposit, withdraw, check balance, or log out. Each time the user performs one of these actions, it...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
Write down the C++ Program To Find Factorial.
Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT