Question
Once you've compiled and tested your code, submit your completed .java files (you'll have 2 files). First, create a class LetterCount that: 1) stores a
Once you've compiled and tested your code, submit your completed .java files (you'll have 2 files).
First, create a class LetterCount that:
1) stores a single character (a letter) and a count (an integer) in private variables
2) implements the Comparable interface, thus there must be a compareTo() method which should compare two LetterCount objects by their count values
3) overrides toString() with a printable representation that shows the letter and the count
Second, create a program with a main() method that:
1) Asks the user for a filename of a text file (assumed to be in the same directory) and create a Scanner to read from it. Your program should use try-catch blocks to re-prompt the user if the file name is invalid.
2) Create an array to store letter frequency counts (# of a's, # of b's, # of c's, etc.). Then, read in the entire specified file and count the number of each letter seen. Ignore all punctuation and count lowercase and capital letters the same (x or X each count the same).
3) Create a LetterCount object for each letter and corresponding frequency count and put all of these objects in an array.
4) Use Arrays.sort() to sort the LetterCount objects. Then use a for-loop to print out all the LetterCount objects from the array, in reverse, so that we can see the frequency of each letter from the file, in order of frequency (from most to least frequent)
Notes: + the compareTo() method in LetterCount should return -1 if "this" LetterCount has a smaller count than ther "other" LetterCount, given by the argument to the method. It should return 0 if they're equal, and return 1 otherwise. + To read from the file, use a while-loop with the hasNext() method of the Scanner on the file. Then read it in to a String variable, one line at a time, using the nextLine() method. Then, process this String one character at a time using a for-loop and the charAt() method + one way to do the letter counting: set up a String of all the letters: "abcdefghijklmnopqrstuvwxyz" and use the indexOf() method on this String to look up a letter from the alphabet. If the indexOf() value is -1, the character is NOT a letter so ignore it. If it is not -1, use the index given to increment the count in an array of 26 int values (each representing the count of the corresponding letter)
To test your program you can use the attached text file, wonderland.txt. (<-- this is just a file with Alice in Wonderland in text) The output should be:
Letter e was seen 15395 times. Letter t was seen 12200 times. Letter a was seen 9802 times. Letter o was seen 9477 times. Letter i was seen 8633 times. Letter n was seen 8051 times. Letter h was seen 7889 times. Letter s was seen 7268 times. Letter r was seen 6610 times. Letter d was seen 5469 times. Letter l was seen 5211 times. Letter u was seen 3978 times. Letter c was seen 3000 times. Letter w was seen 2952 times. Letter g was seen 2943 times. Letter y was seen 2584 times. Letter m was seen 2467 times. Letter f was seen 2382 times. Letter p was seen 1968 times. Letter b was seen 1746 times. Letter k was seen 1290 times. Letter v was seen 963 times. Letter j was seen 235 times. Letter q was seen 220 times. Letter x was seen 176 times. Letter z was seen 80 times.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started