Question: Hey I need help with a java program. Comments would be helpful! Thank you so much.The text file is Mary had a little lamb, BY
Hey I need help with a java program. Comments would be helpful! Thank you so much.The text file is
"Mary had a little lamb," BY SARAH JOSEPHA HALE Mary had a little lamb, Its fleece was white as snow; And everywhere that Mary went The lamb was sure to go. It followed her to school one day, Which was against the rule; It made the children laugh and play To see a lamb at school. And so the teacher turned it out, But still it lingered near, And waited patiently about Till Mary did appear. Why does the lamb love Mary so? The eager children cry; Why, Mary loves the lamb, you know, The teacher did reply.
In this class you are going to simulate the creation of a dictionary using lists to count the frequency that words occur in a file.
You will create an ADT Dictionary which contains a series of Entries that represent specific words and the # of times they occur in the input file.
To accomplish this, you will first create a ADT Entry that contains the information about a word with the following attributes:
word - String representing a word in the dictionary
number of occurrences - the number of times the word occurred as an int.
For Class Entry you will need the following methods:
public Entry( String aWord) : this creates an Entry instance containing the specified word, and sets the number of occurrences to 1
public String getWord(): This returns the word contained in a given entry.
public int getNumber(): this returns the number of times this word has occurred in the text
public void incrementNumber(): this method adds 1 to the number of occurrences for this word.
public boolean equals( Entry b): This method overrides the equals method inherited from class Object. It returns true if the word contained in this entry is the same as the word contained in the parameter b.
public int compareTo( Entry b): This method compares the words stored in each entry and calls the compareTo method for class String to correctly order the words in the dictionary.
CLASS ENTRY MUST IMPLEMENT THE COMPARABLE INTERFACE.
Class Dictionary
This class uses class ArrayList which is part of the Java API. Class ArrayList is an implementation of a List ADT. It encapsulates the operations for a List we discussed in class, along with many more. See the Java API for a complete list of ArrayList Operations.
The Dictionary class will have a data member wordList which is an ArrayList of 26 elements (the number of characters in the alphabet). Each element in the ArrayList will be an instance of class LinkedList whose elements are of type Entry. Words that begin with the letter a will be stored in the first element of thewordList, words that begin with b will be stored in the second , and so forth.
In addition, the class should have an attribute total that counts the total number of words read from the file. This is incremented as words are added to the dictionary
Note: As words are read from the input file, you will need to ignore case and special characters. An easy way to do this is every time you read a word to do:
aWord = aWord.replaceAll("[^a-zA-Z]", " ");
&
aWord = aWord.toLowerCase();
The first statement replaces all non alphabetic characters with whitespaces. Single quotes are only used in possessive nouns. After the previous two statements you will need to trim the string to remove any leading or trailing blanks.
Methods for class Dictionary:
public Dictionary(): Constructor that creates an empty dictionary. It instantiates the array list of array lists as:
ArrayList
public void insert(String word) - a method that stores a word in the dictionary, if it is not already there. If the word already exists it should update its frequency, and update total. The linked list must be BUILT in ascending order. When a word is added to the LinkedList, you must traverse the list to find an entry that either contains the word being added, OR contains a word greater than the word being added. This is where you will use the equals and compareTo methods from class Entry.
public String [] getTopWords(int num) - returns an array of the most popular words in the dictionary. The attribute num specifies the number of words to be returned. difficult
public void printDictionary(): This method prints out all of the words contained in the dictionary along with the number of times the word occurred. If no words for a particular letter existed, just print nothing:
PRINT ALL OF THE WORDS IN THE SORTED DICTIONARY ALONG WITH THEIR FREQUENCY:
a 1000
any 534
apple 10
candle 5
cat 45
.
Tester Class : Main Method
Also write a tester class containing a main method. In the main method which read words one at a time from a tester file. Your main method must:
Create an instance of your dictionary.
Prompt the user for the name of a data file and open a Scanner attached to the data file
Read words one at a time from the data file: [HINT: try something like]
while (dataFile.hasNextLIne() ){
nextWord = dataFile.next();
// then add the word to the dictionary using the method put
}
Once all words are read and added to the dictionary call the printDictionary method, and then print at LEAST the top10 words in the dictionary. Remember getTopWords() returns an array of strings that you will need to print in your main method.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
