Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Frequency Counter We will be developing a program that takes a text file as input and has methods to return the number of total words,

Frequency Counter

We will be developing a program that takes a text file as input and has methods to return the number of total words, the number of distinct words, the number of lines, and keeps track of all the distinct words and their frequency. The name of the file being processed should also be stored in an instance field

1. Create a class named FrequencyCounter

The constructor for this class has the following signature FrequencyCounter(String filename) were filename is the name of the file that will be processed.

The constructor should process the file and gather all the data required as described above and store the data in instance fields for the class

Use another ArrayList to keep track of distinct strings and the number of times each one occurs o To do this, create another class to hold this data

o The instance fields of this class should be private, thus you will need to use getter and setter methods

e.g., this class will need a String instance field, Count instance field and a length instance field.

o The ArrayList<> will be instantiated with this type e.g. if your class was name StringFrequency then the ArrayList declaration would look like this

ArrayList frequencies;

We have not covered exception handling yet, but you will need to use them when opening a file, else the Java compiler will throw an error related to FileNotFoundException, so I will provide a template on how to open a file with a try and catch block below.

import java.io.File;

import java.io.FileNotFoundException;

try {

// create new File object and pass to the scanner

Scanner scan = new Scanner(new File(file));

// Use Scanner methods have you normally have to process the file

// Gather all the data you need in the try block, and store in instance

// fields

} catch (FileNotFoundException e) {

e.printStackTrace();

}

There are two files onCanvas Lab 6 folder, tinyTale.txt and Tale.txt

Download these files, make sure to keep them as txt files, and save them in the same directory as the class file

When creating a FrequencyCounter instance, simply pass the name of the file

FrequencyCounter tinyTale = new FrequencyCounter("tinyTale.txt");

2. After populating all the data in the constructor, create methods to return the following data a. Total number of words

b. Number of Lines

c. Number of distinct words

d. The distinct words, their length and how many times they occur

Example 1:

Here is what the output of what my FrequencyCounter class output for tinyTale.txt:

tinyTale.txt has 60 number of words Method from step c

tinyTale.txt has 5 number of lines. Method from step b

tinyTale.txt has 20 distinct words. method from step a

Below is the output from method from step d

It {Word length: 2}: 10

was {Word length: 3}: 10

the {Word length: 3}: 10

best {Word length: 4}: 1

of {Word length: 2}: 10

times {Word length: 5}: 2

worst {Word length: 5}: 1

age {Word length: 3}: 2

wisdom {Word length: 6}: 1

foolishness {Word length: 11}: 1

epoch {Word length: 5}: 2

belief {Word length: 6}: 1

incredulity {Word length: 11}: 1

season {Word length: 6}: 2

light {Word length: 5}: 1

darkness {Word length: 8}: 1

spring {Word length: 6}: 1

hope {Word length: 4}: 1

winter {Word length: 6}: 1

despair {Word length: 7}: 1

Example 2:

tale.txt has the following data. I excluded results from step d since there is 10679 distinct words, but looks the same as from tinyTale.txt:

tale.txt has 135635 number of words

tale.txt has 16039 number of lines.

tale.txt has 10679 distinct words.

and so on

tinyTale.txt content:

it was the best of times it was the worst of times it was the age of wisdom it was the age of foolishness it was the epoch of belief it was the epoch of incredulity it was the season of light it was the season of darkness it was the spring of hope it was the winter of despair

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

RP-2 What is the best way to predict a persons future behavior?

Answered: 1 week ago

Question

Why are there two stages of allocation in activity-based costing?

Answered: 1 week ago