write a program for me . The purpose of this programming project is to demonstra
ID: 662744 • Letter: W
Question
write a program for me .
The purpose of this programming project is to demonstrate a significant culmination of most constructs learnedthus far in the course. This includes Lists, Classes, accessors, mutators, constructors, implementation ofComparable, Comparator, use of Collections sort, iterators, properly accessing fields of complex objects, andfundamental File I/O.
BACKGROUND
Look over Programming Project #4 at the end of the Searching & Sorting Chapter (13), page 869.Programming Assignment 9 is similar with some added features as described below.You will submit a single file Programming9.zip through the Programming Assignment 9 Submission link onCanvas. This zipped file will contain a minimum of 5 files which make up the solution to this assignment. DONOT zip any folders.The standard grading rules (documentation included) for all previous assignments applies here as well.
DETAILS
To better prepare you for assignments in more advanced courses, specific step-by-step, method headings,class names, and the like have been omitted. What remains is a general description (you may need to readthis several times to fully understand the requirements):You will create a LinkedList of Word objects using all the words found in the input file words.txt. A Wordobject contains 2 String fields; 1 to store a word in its normal form and the other to store a word in its canonicalform. The canonical form stores a word with its letters in alphabetical order, e.g. bob would be bbo, cat wouldbe act, program would be agmoprr, and so on. The class Word constructor has the responsibility of storingthe normal form of the word in the normal form field and converting the normal form into the canonical formwhich is stored in the canonical form field (you should call a separate method for this conversion purpose).Once all the words from the input file have been properly stored in a LinkedList of Word, you should useCollections to sort this list ascending alphabetically based on the canonical words by making the Word classComparable.Using an Iterator on the LinkedList of Word, create a 2nd list (new LinkedList) consisting of objects of a newclass named AnagramFamily. AnagramFamily should contain at least 2 fields; 1 to hold a list of
Explanation / Answer
//importing the packages
import java.io.*;
import java.util.*;
class Word implements Comparable<Word>
{
private String normalForm;
private String canonicalForm1;
//constructor
//converting a word from the normal form to canonical form
public Word(String word)
{
normalForm = word.toLowerCase();
char[] alphabets = word.toCharArray();
Arrays.sort(alphabets);
canonicalForm1 = new String(alphabets);
}
//return the normal form of the word
public String getNormalForm()
{
return normalForm;
}
//return the canonical form
public String getCanonicalForm1()
{
return canonicalForm1;
}
//compare the canonical form
public int compareTo(Word other)
{
return canonicalForm1.compareTo(other.getCanonicalForm1());
}
//overridden toString() method
public String toString()
{
return normalForm;
}
}
//class Anagram_family
class Anagram_Family
{
private List<Word> family1;
//creating new list of type Word
public Anagram_Family()
{
family1 = new LinkedList<Word>();
}
//add a new word to the current-family
public void add(Word word)
{
family1.add(word);
}
//return the family size
public int getSize()
{
return family1.size();
}
// a overridden toString()method
public String toString()
{
return family1.toString();
}
//return the canonicalForm
public String getCanonicalForm1()
{
if(family1.isEmpty())
return null;
else
return family1.get(0).getCanonicalForm1();
}
}
//class that compare the two Anagram family by size
class Compare_FamilySize implements Comparator<Anagram_Family>
{
public int compare(Anagram_Family a, Anagram_Family b)
{
if(a.getSize() > b.getSize())
{
return 1;
}
else if(a.getSize() < b.getSize())
{
return -1;
}
else
return 0;
}
}
//class AnagramsImplemetation is the driver class
public class AnagramsImplementation1
{
//create two list
//1st List is of type word class
//2nd list is of type Anagram_Family
public static List<Word> words = new LinkedList<Word>();
public static List<Anagram_Family> familyList = new LinkedList<Anagram_Family>();
//a mian method for driver class
public static void main(String[] args)
{
//call the generate method to read word from the file
generate_WordList();
//sort the word list
Collections.sort(words);
//generate the anagram family for the word
generate_FamilyList();
//sort the anagram family list
Collections.sort(familyList, new Compare_FamilySize());
//reverse the anagram family list
Collections.reverse(familyList);
//print the top ten families of anagram family
printFamilies();
}//main ends
//method that print the top ten families
/*
*****Print the canconical form*******
*****Print the family Size***********
*****Print the family members********
*/
public static void printFamilies()
{
int i;
for(i = 0; i < 10; i++)
{
System.out.print(familyList.get(i).getCanonicalForm1() + ", ");
System.out.print(familyList.get(i).getSize() + ": ");
System.out.println(familyList.get(i));
}
}
//method that read word
public static void generate_WordList()
{
File inFile12=new File("words.txt");
Scanner fileRead1=null;
try {
fileRead1 = new Scanner(inFile12);
} catch (Exception exe) {
exe.printStackTrace();
System.exit(0);
}
//until the file has words read the words
while(fileRead1.hasNext()) {
words.add(new Word(fileRead1.next()));
}
}
//generate the anagram and add it to the current family
public static void generate_FamilyList()
{
Iterator<Word> readWord1 = words.iterator();
Word previousWord1 = words.get(0);
familyList.add(new Anagram_Family());
int index1 = 0;
while(readWord1.hasNext()) {
Word currentWord1 = readWord1.next();
if(currentWord1.getCanonicalForm1().equals(previousWord1.getCanonicalForm1())) {
familyList.get(index1).add(currentWord1);
} else {
index1++;
familyList.add(new Anagram_Family());
familyList.get(index1).add(currentWord1);
}
previousWord1 = currentWord1;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.