
Joshua S.
asked 10/02/20i need help with this java code
Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain less than 20 words.
Ex: If the input is:
the output is:
Hint: Use two arrays, one for the strings, another for the frequencies.
Your program must define and call a method:
public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord)
Note: This is a lab from a previous chapter that now requires the use of a method.
1 Expert Answer

Patrick B. answered 10/03/20
Math and computer tutor/teacher
//********* WordFreqRec.java *************************//
class WordFreqRec
{
private String word;
private int count;
public WordFreqRec ( String Word)
{
this.word = new String(Word);
this.count = 1;
}
//copy constructor
public WordFreqRec( WordFreqRec wordFreqRec)
{
this.word = new String(wordFreqRec.GetWord());
this.count = wordFreqRec.GetCount();
}
public int GetCount() { return(count); }
public int IncrCount() { return(++count); }
public int DecrCount() { return(--count); }
public String GetWord() { return( new String(word)); }
public void Display( String strMsg)
{
if (strMsg != null)
{
System.out.println("-----------------------------");
System.out.println(strMsg);
System.out.println("------------------------------");
}
System.out.println(" word = >" + this.word + "< : freq count = " + this.count);
}
}
//************ WordFreq.java *********************************//
import java.io.*;
class WordFreq
{
private WordFreqRec dictionary[];
private int count;
WordFreq()
{
dictionary = new WordFreqRec[20];
count=0;
}
public int GetCount() { return(count); }
public WordFreqRec IndexerGetIndexAt( int iIndexPos)
{
WordFreqRec wordFreqRecReturn=null;
if ((iIndexPos>=0) && (iIndexPos<count))
{
wordFreqRecReturn = dictionary[iIndexPos];
}
return(wordFreqRecReturn);
}
public int LinearSearch( String Word)
{
int iIndexPos=-1;
for (int iLoop=0; iLoop<count; iLoop++)
{
if (((dictionary[iLoop]).GetWord()).compareToIgnoreCase(Word)==0)
{
iIndexPos = iLoop;
break;
}
}
return(iIndexPos);
}
public int AddInsertNewWord( String Word)
{
int iReturn=-1;
if (count < 20)
{
dictionary[count++] = new WordFreqRec(Word);
iReturn=0;
}
return(iReturn);
}
public int DeleteRemoveWord(int iIndexPos)
{
int iReturn=0;
if (count>0)
{
if ((iIndexPos>=0) && (iIndexPos<count))
{
for (int iLoop=iIndexPos; iLoop<count-1; iLoop++)
{
dictionary[iLoop]=dictionary[iLoop+1];
}
dictionary[count--]=null;
}
else
{
iReturn -= 2;
}
}
else
{
iReturn -= -1;
}
return(iReturn);
}
public void Go()
{
int numWords;
String inbuff;
Console console = System.console();
WordFreq wordFreq = new WordFreq();
System.out.print(" How many words ??? :>");
inbuff = console.readLine();
numWords = Integer.parseInt(inbuff);
System.out.print(" :> ");
inbuff = console.readLine();
String tokens[] = inbuff.split(" ");
if (numWords == tokens.length)
{
//inputs the words into the array and counts them
int N=numWords=tokens.length;
for (int iLoop=0; iLoop<N; iLoop++)
{
int iIndexPos = wordFreq.LinearSearch(tokens[iLoop]);
if (iIndexPos>=0)
{
(wordFreq.IndexerGetIndexAt(iIndexPos)).IncrCount();
}
else
{
wordFreq.AddInsertNewWord(tokens[iLoop]);
}
} //for
//displays the dictionary
N = wordFreq.GetCount();
for (int iLoop=0; iLoop<N; iLoop++)
{
(wordFreq.IndexerGetIndexAt(iLoop)).Display(null);
}
} //# of words counted correctly
else
{
System.out.println(" Invalid input : # of words is incorrect ");
}
} //Go
public static void main(String args[])
{
WordFreq x = new WordFreq();
x.Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code is uploaded in the RESOUCES section for you under this link. Filename is word freq code It contains the two source code files: WordFreqRec.java and WordFreq.java10/03/20