Create a word class that contains two instance variables


Problem

Project Gutenberg has been providing free copies of books on the Internet since at least 1989. For your next exercise, we would like you to tell us how many distinct words there are in the enclosed text file which is a plaintext copy of the King James Bible, available from Project Gutenberg. The file is provided in the src directory (assume this is the working directory). If you run your program with eclipse, you will need to set the working directory to this src directory.

Write a program that meets the following requirements:

I. Create a Word class that contains two instance variables, a String for the word and an int representing its frequency.

II. Word must contain a constructor that accepts one parameter, a String (which is the word). The constructor should set the frequency to one.

III. Make the String in Word final. Its value will be assigned in the constructor and never changed. Provide an accessor for the String. Provide an accessor for the frequency. Also implement a method that increments the frequency by 1.

IV. Word should have a toString method that returns the word and its frequency.

V. Word should also implement the Comparable interface. By implementing the interface, Word must contain a method called compareTo which accepts another Word as the parameter and returns an int. When comparing Words, the word with the higher frequency should come first.

VI. Creat a WordCounter class with a List of Word as an instance variable. Initialize this with an ArrayList.

VII. WordCounter should contain a method called parseBook which accepts a String as a parameter which represents the name of the file to open. The parseBook method will open the file using a Scanner and use a while loop to acquire each word. Your program should check if the word is already in the list. If it is, itsfrequency should be incremented. If the word is not in the word list, then it should be added. When parseBook finishes, it should return the total number of unique Words stored in the list.

VIII. WordCounter should have a method called printTopWords which accepts an int n and then prints the n most frequent words in order (most frequent ones first). Make sure you sort the List first. Note that calling the List sort method with a null parameter will sort the list according to Word's compareTo method.

IX. You will need a main method to drive the program. Put it at the bottom of the WordCounter class (inside the class, but after all the other methods). Inside the main method, instantiate a WordCounter. Invoke the Word Counter object's parseBook method passing it the name of a file, and then print the result (which will be the number of words in the text). Finally, print out the top 10 most frequent words.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Create a word class that contains two instance variables
Reference No:- TGS03261643

Expected delivery within 24 Hours