Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class named ResultsEntry containing the following:Private instance variablesnamedcount(int) and target(char)A single constructor with the two valuesPublic getmethods for the two variablesA public toString()method

Create a class named ResultsEntry containing the following:Private instance variablesnamedcount(int) and target(char)A single constructor with the two valuesPublic getmethods for the two variablesA public toString()method that returns a string in the format target,count

and this is my code

public class ResultEntry { //creating the private instance private int count; private char target; //creating the default constructor public ResultEntry() { } //Creating the constructor with two values public ResultEntry(int count, char target) { this.count = count; this.target = target; } //Creating public get methods for the two variables public int getCount() { return this.count; } public char getTarget() { return this.target; } //Creating toString method public String toString() { return "" + target + count; }

} Create a class named SharedResults containing the following:Private instance variable named resultsof ArrayListtypeDefault constructor that initializes resultsvoid add ToResults()method thattakes aResultsEntryargument and adds it to the end of results, then prints to the console the name of the current thread, the entry it addedandtheresultsdata structure. Handle synchronization with this method. AgetResult()method with no arguments thatreturns the sum of the count entry values in resultsdata structure. Handle synchronization with this method.

this is my code

import java.util.ArrayList;

import java.util.List;

public class SharedResults {

//Create the private instance as an ArrayList

private List results = new ArrayList();

//Create the default constructor

public SharedResults() {

}

//Create void method ToResults

public synchronized void ToResults(ResultsEntry resultEntry) {

//start try/catch

try {

//add the argument to results

this.results.add(resultEntry);

/*

* printing the name of the current thread

* the entry it added and the results data Structure

*/

System.out.println("Thread " + results + " " + resultEntry+ "" + this.results);

}catch(InterruptedException e) {

e.printStackTrace();

}

}

//creating public method getResult

public synchronized int getResult() {

int sum =0;

for (int i=0; i<=results.size();i++) {

results.get(i);

return sum += results.get(i) ;

}

Create a class named LongTaskthat extends the Threadclasscontaining the following:Private instance variablessharedData(of type SharedResults), inputData(of type StringBuffer)and target(of type char) Constructor thattakes three arguments and stores them in the instance variables.It should alsocreate a name for this thread as Thread_.In the run()method, use a loop overthe characters ininputDataand count the number of occurrences of target. After the loop is done, create a ResultsEntryobject with this count and the target character, and invoke the addToResults()methodof results

this is the code

public class LongTask extends Thread { //Create private instance variables SharedData, inputData, target private SharedResults sharedData; private StringBuffer inputData; private char target; //create constructor with three arguments public LongTask(SharedResults sharedData, StringBuffer inputData, char target) { this.sharedData = sharedData; this.inputData = inputData; this.target = target; //it should also create a name for this thread as Thread_; } //create run() method public void run() { /*Create for loop for looping over the characters in inputData and count the number of occurrences of target * * */ for (int i =1; i

} I stuck at the for loop I didn't know what to do and what is this it should also create a name for this thread as thread_

here I lost and I couldn't complete it?? any help??

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

Students also viewed these Databases questions

Question

3. Is IBMs program really a mentoring program? Why or why not?

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago