words.txt url: http://www.buildingjavaprograms.com/code_files/3ed/ch13/words.txt
ID: 3770355 • Letter: W
Question
words.txt url: http://www.buildingjavaprograms.com/code_files/3ed/ch13/words.txt
the program on the textbook’s site url: http://www.buildingjavaprograms.com/code_files/3ed/ch10/Vocabulary1B.java
2) Start with the words.txt provided in the Chapter 13 files on the author’s website. Download it to whatever directory you are going to write code in. Now write a programFindAnagrams.java that (in its main method) reads words.txt, and for each word there, adds it into an ArrayList words and its ordered form into an ArrayList codes. It obtains the ordered form by calling the same orderedForm method you developed for OrderedWord.java. Sort the codes ArrayList using Collections.sort. Then hunt for duplicates in the sorted ArrayList codes. Any duplicates there will indicate different words which are anagrams for each other in the original words ArrayList. Keep track of the code with the most duplicates so far as you scan through the codes. Finally using this code with the most duplicates, scan the words list for words that have this as their ordered form and output them. Don’t worry about ties for most duplicates, just report on the first anagram group with the most duplicates in the list of codes.
Test case (this is just an example, the actual words.txt you will be working with is MUCH longer):
words.txt codes sorted codes
rat art abt
hears aehrs abtt
share aehrs aehrs
tar art aehrs
bat abt aehrs
batt abtt art
shear aehrs art
The triplet of codes “aehrs” means that the corresponding words (hears, share, and shear) are anagrams. Similarly the duplicate of codes “art” indicates that rat and tar are anagrams. We are interested in the longest anagram, so the program should output as follows:
java FindAnagrams
hears
share
shear
We see there are two anagram groups here, one with code aehrs and the other with code art, so the one with the most duplicates is aehrs, corresponding to words hears, share, and shear.
For an example of comparing words next to each other in a sorted list see line 34 of Vocabulary1b.java, pg. 684, or line 30 of the program on the textbook’s site. Note that this program also show examples of reading a file contaning words into an ArrayList, converting words to lower-case, sorting an ArrayList of Strings, and ensuring there are no duplicates in a sorted ArrayList. Many of these tasks are necessary for this project. Also note that we are assuming that all the words inwords.txt are in the same case, uppercase or lowercase. The textbook’s words.txt is all in lowercase.
the code that i have so far is this: i hope it helps!!! thanks
import java.io.*;
import java.util.*;
public class OrderForm
{
public static char[] OrderedWord(String s)
{
StringReader sr = new StringReader(s);
char c[] = new char[20];
try {
for (int i = 0; i < 20; i++)
{
c[i] = (char) sr.read();
}
} catch (IOException ex)
{
ex.printStackTrace();
}
sr.close();
Arrays.sort(c);
return c;
}
public static void main(String[] args)
{
String s = "Hello World";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
s =sc.nextLine();
char ch[] = new char[20];
ch = OrderedWord(s);
for (int i = 0; i < s.length(); i++)
{
System.out.print( ch[i] );
}
}
}
Explanation / Answer
FindAnagrams.java
public static void main(String args[])
{
/* Scanner object to read words from file */
Scanner in = new Scanner("words.txt");
/* ArrayList to store the words */
ArrayList<String> wordList = new ArrayList<String>();
/* Reading & Storing the words in the ArrayList */
String word = null;
while(in != null)
{
word = in.next();
wordList.add(word);
}
/* Sorting ArrayList */
wordList.sort();
/* Beginning to track the duplicates/anagram in the list
The hashmap will store the sorted string as key, and its corresponding anagrams as value through LinkedList
*/
LinkedList<String> ll = new LinkedList<String>(100);
HashMap<String, ll> wordMap = new HashMap<String, ll>();
Iterator it = wordList.iterator();
while(it.hasNext())
{
String temp_orig = it.next();
char t1[] = temp.toCharArray();
t1.sort();
String temp_sorted = t1.toString();
if(wordMap.get(temp_sorted) == null)
{
ll.add(temp_orig);
wordMap.put(temp_sorted, ll);
}
else
{
wordMap.get(temp_sorted).add(temp);
}
}
/* Above code generates a HASHMAP containing LinkedList of Anangrams */
/* Print the Hasmap */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.