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

The word-frequency problem of Segment 19.12 finds the frequency with which each

ID: 3839402 • Letter: T

Question

The word-frequency problem of Segment 19.12 finds the frequency with which each distinct word occurs within some given text. Describe the changes that you could make to the class FrequencyCounterif you wanted to list the words that occur for each frequency

A Problem Solved: The Frequency of Words Some word pooesson provide a oount of the number of times each word ocaus Greate aclass Frequencycounter that provides this capability. 19.12 This class is s omewhat like the one in the previous e, so we will omitsame of the desigm detaik. Basically, the dass needs to count each ocaumence of a word as it reads he document form textfile. Itthen needs to display the results. For example, if the text file contains row, row, row your boat the desired ouput would be boat 1. your 1 The class will have a constructor and the methods readFile and display As in the previ ample, readFi e will read the imput text from a file.Then display will write the ou put. Listing 19-4 shows a client of FrequencyCounter. It is similar to the inning of he client in the previous example. LISTING 19-4 A client of the class Fr tinport Java-ut11.Scanner: inport Java. 10. F11e: import Java-10-F11eNotFoundExcept1on: using the ADT Dictonary 485 Port Java. 10.IOExcept 1on: public class Driver public static void main(String[] args) Frequency Counter wordCounter new FrequencyCountero: String f11eName Data txt" e name could be read Scanner data Scanner (new F11ecf11eName)) WordCounter-readF11e (data) catch CF1leNotFoundExcept1on e) System-out. Printlnc F11e not found e.getMessage O) catch CIOExcept1on e) System-out.printlnCI/0 error e.getMessage()) WordCount er-display O system-out. PrintlnC"Bye!") end main end Driver

Explanation / Answer

import java.io.*;
class FrequencyCounter{
public String st="";
  

public void readfile()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence");
st=br.readLine();
}

void display(){
int i,j,k,count=0,c=0,m,n,flag=0;
char ch;
String word="";
st=st+" ";
i=st.length();
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch==' ')
count++;
}
String arr[]=new String[count];
int pos=0;
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch!=' ')
word+=ch;
else
{
arr[pos++]=word;
word="";
}

}
for(m=0;m<pos;m++)
{
flag=0;
for(n=0;n<m;n++)
{
if(arr[m].compareTo(arr[n]) == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
c=1;
for(k=m+1;k<pos;k++)
{
if(arr[m].compareTo(arr[k]) == 0)
c++;
}
System.out.println(arr[m]+" => "+c);
}
}
}

}

Now you can create a main() function in the Driver class.