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

This assignment is a variation of Listing 21.9, CountOccurrenceOfWords. Instead

ID: 3713247 • Letter: T

Question

This assignment is a variation of Listing 21.9, CountOccurrenceOfWords. Instead of a program, you will write two classes, one that performs the analysis and the other that captures the results. The TestHarness will use the results class, WordCountResult, to evaluate for correctness. There is no GUI for this assignment.

Implementation Details:

Step 1

Create week04 package

Implement the WorkCountResult class. This class encapsulates the results of a word count search.

Custom to string that generates the following text: count word

For example: 5    the

Step 2

Implement the CountUtility class. This class provides methods to count words in text and eventually other counting functions.

Evaluates an individual line by splitting into the words.
Checks to see if each word has previously been mapped and if so, increment the occurrence count, otherwise add and set count to 1.

Use a Map class to hold the contents of the evaluation. For example Map where the String is the key and the WordCountResult is an instance of a word and its occurrences.

Turn In:

CountUtility.java

WordCountResult.java

Method Description Notes WordCountResult(int count, String word) Constructor that initializes an instance Used when a new word has been found that needs to be added to the occurrence list getCount() Returns the number of occurrences of the word getWord() returns the word being analyzed incrementCount() Increments the occurrence count Used when the word has been added already and a new occurrence has been found setCount(int count) Used to set the occurrence count. Replaces any existing value. This isn't used frequently; general usage is to create a new instance with count 1 when a new word is encountered setWord(String word) Used to set the word. Replaces the existing word This isn't used frequently. toString()

Custom to string that generates the following text: count word

For example: 5    the

Format syntax: "%d %s" class week04 WordCountResult m_count: int + getCount): int + getWord): String + incrementCount): void + setCount intt void + setWord(String): void + toStringl): String + WordCountResult(int, String)

Explanation / Answer

Have created a package named "com.assignment.week04"

Here are the 3 classes that is tested thoroughly.

Explanation is given in the comments.

WordCountResult:

==============

package com.assignment.week04;

// model class to depict a word and its count

public class WordCountResult {

private int mCount;

private String mWord;

// default constructor that does nothing

public WordCountResult() {

super();

}

// parameterized constructor that initializes the words with the params passeed

public WordCountResult(int mCount, String mWord) {

super();

this.mCount = mCount;

this.mWord = mWord;

}

// Getters and setters for the fields

public int getmCount() {

return mCount;

}

public void setmCount(int mCount) {

this.mCount = mCount;

}

public String getmWord() {

return mWord;

}

public void setmWord(String mWord) {

this.mWord = mWord;

}

// increments the existing count by 1

public void incrementCount(){

this.mCount += 1;

}

// prints the count and the word

@Override

public String toString() {

return mCount+" "+mWord;

}

}

CountUtility:

=========

package com.assignment.week04;

import java.io.BufferedReader;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import java.util.TreeMap;

public class CountUtility {

// method to read a line and analyze the word count and return a list of words and their counts

public static List<WordCountResult> countWordOccurrences(BufferedReader reader) throws Exception{

// Ask user to enter a sentence or some text

System.out.println("Please enter the text or a line");

// read a line from the reader passed

String line = reader.readLine();

// create a map that has word as a key and its word occurrence representation WordCountResult as value

Map<String,WordCountResult> occurrences = new TreeMap<>();

// pass this to a local method evaluateLine that has the actual logic find the count of occurrences of

// each word in the line and fill the map

evaluateLine(line, occurrences);

// Loop thru the map and add the values or WordCountResult objects to a list and return

Collection<WordCountResult> valuesCollection = occurrences.values();

// create a list to return

List<WordCountResult> wordCountResultList = new ArrayList<WordCountResult>();

// check for empty condition which occurs when the line read has no text in it

if(!valuesCollection.isEmpty()){

for(WordCountResult wordCountResult : valuesCollection){

wordCountResultList.add(wordCountResult);

}

}

return wordCountResultList;

}

// Analyze the line and fill the map with analysis

private static void evaluateLine(String line,Map<String,WordCountResult> occurrences){

// split the line into words by passing delimiters to split method

String words[] = line.split("[ ,.;:!?(){}]");

// loop thru the words and modify the map with the counts

for(int index=0;index < words.length;index++){

// get the current word

String word = words[index].toLowerCase();

// check if the word has at least 1 char

if(word.length() > 0){

// check if the word is already in the map

if(occurrences.containsKey(word)){

// if yes, increment the count

WordCountResult wordCountResult = occurrences.get(word);

// calling incrementCount will increment the count for the word

// as wordCountResult is a reference, this change is affected in the map also

wordCountResult.incrementCount();

}else{

// if not, let us create a WordCountResult object with count as 1

// and add it to map

WordCountResult wordCountResult = new WordCountResult(1,word);

occurrences.put(word, wordCountResult);

}

}

}

}

}

TestHarness:

==========

package com.assignment.week04;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.List;

public class TestHarness {

/**

* @param args

*/

public static void main(String[] args) {

// to test count utlity, create a reader from console and pass it to countWordOccurrences method

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

// catch the exception

try{

// get the word count results

List<WordCountResult> wordCountResultList = CountUtility.countWordOccurrences(reader);

// check for empty condition

if(!wordCountResultList.isEmpty()){

for(WordCountResult wordCountResult : wordCountResultList){

System.out.println(""+wordCountResult);

}

}

}catch(Exception exception){

// print the exception

exception.printStackTrace();

}

}

}