Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please follow the instruction the language is Java Designing an encapsulated Java class: In the example FrequencyCounter.java in the following code the word with highest

Please follow the instruction the language is Java

Designing an encapsulated Java class: In the example FrequencyCounter.java in the following code the word with highest frequency is found, along with its count. This result could be packaged up in one object with two instance variables word and count. For example if the showed up 2034 times in the text, this object would have word the and count 2034. Create such a class, named WordUsage, with a constructor taking both the word and the count, and another constructor taking only the word and using count 1. Make the instance variables private for proper encapsulation of the objects. Give the class getters for word and count (i.e. getWord and getCount) and a setter for count but not word (setCount) and a mutator method named increment that adds one to the count. Dont implement equals or other Object methods here: this is a simple object meant for just carrying data from one place to another in our code. Note the strong Java convention that class names are capitalized, but method names and variables start with lower case. See pp. 84-85 of S&W for discussion of a similar class implementation. Here we can also mark the String instance variable as final because we cant change it once the object is created, since there is no setter for the word, and the instance variables are private, protected against direct access.

/****************************************************************************** * Compilation: javac FrequencyCounter.java * Execution: java FrequencyCounter L < input.txt * Dependencies: ST.java StdIn.java StdOut.java * Data files: https://algs4.cs.princeton.edu/31elementary/tinyTale.txt * https://algs4.cs.princeton.edu/31elementary/tale.txt * https://algs4.cs.princeton.edu/31elementary/leipzig100K.txt * https://algs4.cs.princeton.edu/31elementary/leipzig300K.txt * https://algs4.cs.princeton.edu/31elementary/leipzig1M.txt * * Read in a list of words from standard input and print out * the most frequently occurring word that has length greater than * a given threshold. * * % java FrequencyCounter 1 < tinyTale.txt * it 10 * * % java FrequencyCounter 8 < tale.txt * business 122 * * % java FrequencyCounter 10 < leipzig1M.txt * government 24763 * * ******************************************************************************/ package edu.princeton.cs.algs4; /** * The {@code FrequencyCounter} class provides a client for * reading in a sequence of words and printing a word (exceeding * a given length) that occurs most frequently. It is useful as * a test client for various symbol table implementations. * 

* For additional documentation, see Section 3.1 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class FrequencyCounter { // Do not instantiate. private FrequencyCounter() { } /** * Reads in a command-line integer and sequence of words from * standard input and prints out a word (whose length exceeds * the threshold) that occurs most frequently to standard output. * It also prints out the number of words whose length exceeds * the threshold and the number of distinct such words. * * @param args the command-line arguments */ public static void main(String[] args) { int distinct = 0, words = 0; int minlen = Integer.parseInt(args[0]); ST st = new ST(); // compute frequency counts while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (key.length() < minlen) continue; words++; if (st.contains(key)) { st.put(key, st.get(key) + 1); } else { st.put(key, 1); distinct++; } } // find a key with the highest frequency count String max = ""; st.put(max, 0); for (String word : st.keys()) { if (st.get(word) > st.get(max)) max = word; } StdOut.println(max + " " + st.get(max)); StdOut.println("distinct = " + distinct); StdOut.println("words = " + words); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions