Question: Pls help me for the following step. 2. Now start on LineUsage. It is supposed to use a Map of String to Integer to hold

Pls help me for the following step. 2. Now start on LineUsage. It is supposed to use a Map of String to Integer to hold the counts so far for each user seen on a particular line. Thus if OPERATOR was seen on the line 34 times and USERMGR seen 12 time, the map would hold "OPERATOR" --> 34 ad "USERMGR" --> 12. You could find the 34 by calling map's get method, get("OPERATOR"). Use the Map type for the type of the instance variable, but of course use you need to use the concrete type HashMap or TreeMap for creating the container object.

3. Study the supplied program FrequencyCounter.java to see the details of repeatedly adding one to the count for a certain string. Here the map itself is held in an instance variable of LineUsage, private of course, not a local variable of method main as you see in FrequencyCounter.java. Thus each LineUsage object has its own map for its own data, separate from the data for a different line number. The constructor of LineUsage creates an instance of the map for the value of this instance variable.LineUsage should have methods addObservation(String username) and Usage findMaxUsage(), which returns the Usage object for the user with the highest count. Note that Usage is not used in the map, only in the delivery of results once the Map is full of data. here is the previous program

public class Usage { // These are the private members private String username; private int count; // This is a parameterised constructor public Usage(int count, String username) { this.count = count; this.username = username; } // To get the username public String getUsername() { return this.username; } // To get the count public int getCount() { return this.count; } // To increment the count public void increment() { count++; } public static void main(String args[]) { // Here we make the object with count as 5 and username as MyUser Usage obj = new Usage(5, "MyUser"); // Apply the increment method obj.increment(); // Now show the value using getCount and getUsername function System.out.println("The count value is " + obj.getCount()); System.out.println("The username is " + obj.getUsername()); } } ___________________________________________________________________________________ and FrequencyCounter.java 
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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!