Write a new class named Assignment3. Requirements: 1. Add two private member var
ID: 3685339 • Letter: W
Question
Write a new class named Assignment3.
Requirements:
1. Add two private member variables, one to store the name of the file, and another to store the count of words in the file.
2. Add a public constructor. Its only argument is the name of the file. Use it to initialize one of the variables from requirement #1.
3. Add a public "getter" for the word count, name it "getWordCount".
4. Add a public function named "processFile". It returns nothing and accepts no arguments. This function will:
a) Open the file.
b) Read the file's contents into a buffer.
c) Close the file.
d) Convert the buffer into a string.
e) Tokenize the string and use the tokenizer to initialize your word count variable.
5. Add a private member variable that will store the most frequent word in the file.
6. Add a "getter" for the variable (of #5)
7. Add code to your processFile function that counts the number of times each word appears in the file. A HashMap is a good choice for this.
8. Add code to your processFile function that loops through your HashMap (if that's what you use) to find the most frequent word. After your loop, the variable added (for #5) should contain the value for the most frequent word. Don't worry about ties
Explanation / Answer
Hello there,
Please find below code,I/P and O/P.
package com.dipal;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
/**
* This is the word count class .
*
* @author dipal.prajapati
*
*/
public class Assignment3 {
private String nameOfFile;
private int wordCount;
private String mostFrequentWord;
public Assignment3(String nameOfFile) {
super();
this.nameOfFile = nameOfFile;
}
public int getWordCount() {
return wordCount;
}
public String getMostFrequentWord() {
return mostFrequentWord;
}
/**
* It reads the given file and process it by tokenising and counting the
* word's frequency and sets the mostFrequentWord.
*
* @throws IOException
*/
@SuppressWarnings("unchecked")
public void processFile() throws IOException {
BufferedReader br = null;
StringBuffer buffer = null;
Map<String, Integer> wordCountMap = new HashMap<String, Integer>(); // To
// sort
// we
// have
// taken
// TreeMap
br = new BufferedReader(new FileReader(nameOfFile));
String sCurrentLine;
buffer = new StringBuffer();
while ((sCurrentLine = br.readLine()) != null) {
buffer.append(sCurrentLine + " ");
}
br.close();
StringTokenizer tokenizer = new StringTokenizer(buffer.toString());
wordCount = tokenizer.countTokens();
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
if (wordCountMap.containsKey(word))
wordCountMap.put(word, wordCountMap.get(word) + 1);
else
wordCountMap.put(word, 1);
}
TreeMap<String, Integer> result = new TreeMap<String, Integer>(new ValueComparator(wordCountMap));
result.putAll(wordCountMap);
mostFrequentWord = result.firstKey();
}
}
@SuppressWarnings("rawtypes")
class ValueComparator implements Comparator {
Map<String, Integer> map;
public ValueComparator(Map<String, Integer> map) {
this.map = map;
}
public int compare(Object keyA, Object keyB) {
Comparable valueA = map.get(keyA);
Comparable<Integer> valueB = map.get(keyB);
return valueB.compareTo((Integer) valueA);
}
}
====
package com.dipal;
import java.io.IOException;
/**
* Main class to test Assignment 3 (Word Count program).
* @author dipal.prajapati
*
*/
public class Assignment3Driver {
public static void main(String args[]) {
Assignment3 a3 = new Assignment3(args[0]);
try {
a3.processFile();
System.out.println("There are " + a3.getWordCount() + " words in the file " + args[0]);
System.out.println("The most frequent word in " + args[0] + " is: " + a3.getMostFrequentWord());
} catch(IOException e) {
e.printStackTrace();
}
}
}
I/P: textFile.txt
Hello How are you ?
I am fine.
Hello Again.
I am going to market to shop Hello there ? Hello.
O/P:
There are 21 words in the file textFile.txt
The most frequent word in textFile.txt is: Hello
Let me know if you any queries.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.