- 13th Feb 2024
- 21:40 pm
- Adan Salman
Java Assignment Question
In this assignment, you will produce a list of all words that have 6 or more anagrams. You will create a new class (LinkedDictionary that inherits from WordList as follows:
WordList |
|
– WordNode head |
head of linked list |
+ WordList() |
creates empty list |
+ void display() |
show contents of list |
+ void insert(Word w) |
insert w to front of list |
+ Word search(Word w) |
search for w in list; returns reference |
+ Word remove(Word w) |
remove first occurrence of w if exists |
LinkedDictionary |
|
|
|
+ LinkedDictionary(String f) |
read words from file and insert into list |
+ int countAnagrams(Word w) |
look through list and return count of w's anagrams |
+ void displayBigAnagramFamilies() |
display each word with 6+ anagrams |
For testing purposes you might use a hand-crafted data file with 20 or so words in it (some of which are anagrams). Once you have the program working you can use the provided data file found in the repository: data/mywords.txt (which has over 350,000 words).
You will also need to add a method to Word that will accept another Word object as a parameter and will return true if they are anagrams (false if not). We will use this function rather than.equals() because the latter has already been written to compare the words themselves rather than the sorted versions of the words.
Your solution should use the design specified above. The driver should create a LinkedDictionary and then display its big (6 or more) anagrams. You should add code in main to time actions of the program and display the result at the end. NOTE: When you run the program using the large dictionary file it will take quite a bit of time to finish.
Java Assignment Solution
import java.util.*; import java.io.*; class Word { String s; Word(String str) { s=str; } Boolean isAnagram(Word w) { char s1[]=this.s.toCharArray(), s2[]=w.s.toCharArray(); Arrays.sort(s1); Arrays.sort(s2); if(s1.length!=s2.length)return false; for(int i=0;i<s1.length;i++) { if(s1[i]!=s2[i])return false; } return true; } } class WordList { static class WordNode { Word data; WordNode next; WordNode(Word d) { data = d; next = null; } } WordNode head; public void display() { WordNode currNode = head; while (currNode != null) { System.out.print(currNode.data + " "); currNode = currNode.next; } System.out.println(); } public void insert(Word data) { WordNode new_node = new WordNode(data); new_node.next = head; head=new_node; } public Word search(Word w) { WordNode currNode=head; while(currNode!=null) { if(currNode.data==w)return currNode.data; currNode=currNode.next; } return null; } public Word remove(Word key) { WordNode currNode = head, prev = null; if (currNode != null && currNode.data == key) { Word te=currNode.data; head = currNode.next; return te; } while (currNode != null && currNode.data != key) { prev = currNode; currNode = currNode.next; } if (currNode != null) { Word te=currNode.data; prev.next = currNode.next; return te; } if (currNode == null) { return null; } return null; } } class LinkedDictionary extends WordList { LinkedDictionary(String f)throws Exception { FileReader file = new FileReader(f); BufferedReader br = new BufferedReader(file); String line; while((line = br.readLine()) != null) { String words[] = line.split(" "); for(int i=0;i<words.length;i++) { insert(new Word(words[i])); } } // System.out.println(ll.size()); br.close(); } int countAnagrams(Word w) { int cnt=0; WordNode currNode=head; while(currNode!=null) { if(w.isAnagram(currNode.data)) { cnt++; } currNode=currNode.next; } return cnt; } void displayBigAnagramFamilies() { WordNode currNode=head; while(currNode!=null) { String s=currNode.data.s; Boolean is=true; if(s.length()<3)is=false; else{ if(s.charAt(0)==s.charAt(1) && s.charAt(1)==s.charAt(2))is=false; } if(is) { System.out.println(s); } currNode=currNode.next; } } } public class Solution { public static void main(String args[])throws Exception { LinkedDictionary ld=new LinkedDictionary("mywords.txt"); long start = System.currentTimeMillis(); ld.displayBigAnagramFamilies(); // ld.display(); long end = System.currentTimeMillis(); System.out.println("Time Taken : "+(end-start)/1000+" seconds"); } }