Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java . the text files: [https://files.fm/u/cyixeoq] Write a program that read

ID: 3764246 • Letter: I

Question

In Java.

the text files: [https://files.fm/u/cyixeoq]

Write a program that reads a text file and counts the number of times each English word appears. Display the 25 most frequently used words. The program requires two classes, the main program and a separate class called UseCount to hold a word and its count. The UseCount class needs two instance variables, one String to hold the English word and an int to hold the count of times this word appeared in the text. A constructor for UseCount should have one parameter to initialize the English word. The count should be initialized to one. The main program needs to sort an array of UseCount objects. To be sorted, the UseCount class must implement the Comparable interface and include a method compareTo. The class statement for UseCount should look like: and the compareTo method in UseCount should be: The main program should create an array that can hold 10,000 UseCount objects. After asking the user for the input filename, it should read one English word at a time from the file using the next () method of a Scanner object. For each word read, seareh the array of UseCount objects for an object that contains the word just read (ignoring case). If found, increment the count in that object. If not found, create a new object for that word and put it in the array. You will want to keep a count of the number of UseCount objects in the array. After all words have been read from the input file, you can sort the array of UseCount object in descending order by the count. There is a static method to sort an array of objects. where use is your array of UseCount objects and numWords is the number of objects in the array. The first 25 elements of the array will contain the 25 most commonly used words. Print the top 25 words and the number of times they appeared. When your main program uses the next () method of a Scanner object to read a word, it might read a word that has punctuation at the beginning or end. The following method will remove all non-letter characters from the beginning and end of a word. If there are no letters left after removing the punctuation, the method returns null. Your program will need to check if the return value is null. You will want to copy this method into your main program. Call this method right after you read each word in the data file.

Explanation / Answer

import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;

public class Main{

public static void main(String[] args)
{

WordCount wc = new WordCount();
wc.processLine("da do ro ro ro, da do ro ro");
wc.print ();
}
}

import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;


public class WordCount
{
Hashtable ht;
public WordCount ()
{
ht = new Hashtable ();
}

public void processLine (String s)
{
StringTokenizer st = new StringTokenizer (s, " ,.");
while (st.hasMoreTokens()) {
String word = st.nextToken();
processWord (word.toLowerCase ());
}
}

public void processWord (String word)
{
if (ht.containsKey (word)) {
Integer i = (Integer) ht.get (word);
Integer j = new Integer (i.intValue() + 1);
ht.put(word, j);
}
else {
ht.put(word, new Integer (1));
}
}

public void print ()
{
Enumeration enum1 = ht.keys ();
while (enum1.hasMoreElements ()) {
String key = (String) enum1.nextElement ();
Integer value = (Integer) ht.get (key);
System.out.println ("{ " + key + ", " + value + " }");
}
}
}

try this also helps

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote