Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program is by java and follow the following instruction and add on the following code please.! thank you A simple application using JDK collection

The program is by java and follow the following instruction and add on the following code please.! thank you

A simple application using JDK collection classes.

A simple application using JDK collection classes

The main server at your company maintains 500 terminal lines, numbered 1 through 500 inclusive. Every 10 minutes the system appends to a log file the terminal numbers and user name of the person currently logged on, one terminal number and username per line of text. (Not all terminals are in use at every moment.) A fragment of the log file looks like this:

9 ALTEREGO

12 ALIMONY

433 HOTTIPS

433 USERMGR

12. BLONDIE

433 HOTTIPS

354 ALIMONY

This log file shows HOTTIPS was on terminal 433 twice but USERMGR was on that terminal at an intervening time. It also shows that ALIMONY was on terminal 12 at one time and terminal 354 later on. The log does not display the time at which each line was written. Your job is to write a program in Java that meets the following specifications.

The program first reads a log file into memory, storing the input data as it encounters it. Read from a text file! Make sure you know about absolute and relative paths and dont hard-code the file name. User names are ASCII (plain text) strings with no embedded whitespace and a maximum length of 40 characters.

1

After all the data has been read your program should print data about terminal usage: a header line and then one line of output for each terminal showing the terminal number, the most common user of that terminal (in the event of a tie, choose one user), and a count of how many times that user was on that terminal. Here is sample output:

Terminal Most Common User Count

1 OPERATOR 174983

2 HANNIBAL 432

3 . 0

4 SYSMGR 945

... ... ...

Implementation

Use the provided template. You may add other functions if you want, but for the ones that I provided use the exact same function names as in the template. Since the terminals are numbered consecutively, it is easy to use an array with one spot for each terminal (500 entries in this case). Each line needs a container of user-count data, i.e. how many times each user has been seen on that line. The following class should be implemented:

Create a class named Usage to hold a single username and its count together. It is quite simple. It should have getters getUser and getCount (these names specifically). The constructor should get the name, then the number.

Create a class named LineUsage to hold all the data on one particular terminal. In this LineUsage class, use a Map from the JDK (HashMap or TreeMap), mapping each username to its count, an Integer. The maps name should be lines. See the provided program FrequencyCounter.java for an example of using a Map to hold a count for each of many strings. 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.

The top-level class LineReport has an array of LineUsage objects to hold all the data on all the terminal lines. See page 72 of S&W for discussion of an array of objects. LineReport.java should have several methods (at least two object methods or static methods), including a main() to run the program and a method loadData(String fname) which reads the data from the input file. Each method should be commented (a comment above the method to describe what it does).

To test your code you can add a simple test called TestLineUsage, which tests LineUsage: just create one LineUsage object, add data to it, and print it out. Do not submit it.

Since the array count starts from 0, remember to add 1 back to the index before you print.

LineReprt.java

package pa0;

public class LineReport { public static final int NLINES = 500; // Create an array of 500 LineUsage objects // since there are NLINES = 500 individual lines, // numbered 1 to 500, so use array size NLINES+1 private LineUsage[] lines = new LineUsage[NLINES + 1];

public LineReport() { }

// read input data, put facts into lines array void loadData(String fname) { return; }

// given loaded lines array, generate report on lines void generateReport() { return; }

public static void main(String[] args) { LineReport report = new LineReport(); report.loadData(args[0]); report.generateReport(); } }

LineUsage.java

//package com.gradescope.pa0; package pa0;

// LineUsageData.java: Handle one line's data, using a HashMap public class LineUsage {

public LineUsage() { }

// add one sighting of a user on this line public void addObservation(String username) { return; }

// find the user with the most sightings on this line public Usage findMaxUsage() {

return new Usage("Nothing", -1); } }

Usage.java

package pa0; // One user's record on one line: how many times // this user has been seen on this line public class Usage {

public Usage(String x, int count) { }

public void setCount(int x) { }

public String getUser() { return "NOTHING"; }

public int getCount() { return -1; } }

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

What is database?

Answered: 1 week ago

Question

What are Mergers ?

Answered: 1 week ago