Question: I need help to solve this exercise, I have done the following code, I think the LongTask is wrong. I have attached images below that
I need help to solve this exercise, I have done the following code, I think the LongTask is wrong. I have attached images below that explain what I need to solve/ accomplish. Any help would be appreciated. Thank you
Results Entry:
// public class named ResultsEntry
public class ResultsEntry {
// set instance private variables, such as count (int) and target (char)
private int count;
private char target;
// single constructor with the two values
public ResultsEntry(int count, char target) {
this.count = count;
this.target = target;
}
// get methods for the two variables
public int getCount() {
return this.count;
}
public char getTarget() {
return this.target;
}
// public string that returns the method
public String toString() {
String returnValue;
returnValue = "";
return returnValue;
}
}
Shared Results:
import java.util.ArrayList;
public class SharedResults {
// private instance varriable - results (ArrayList of ResultsEntry type)
private ArrayList
// set a default constructor that initializes the above data result
public void SharedResult() {
this.results = new ArrayList
}
//
public synchronized void addToResults(ResultsEntry entry) {
System.out.printf("%s is adding %s, ", Thread.currentThread().getName(), entry);
results.add(entry);
System.out.printf(" Cumulative Results are %s ", results);
}
public synchronized long getResult() {
long sum = 0;
for (ResultsEntry entry : results) {
sum += entry.getCount();
}
return sum;
}
}
Long Task:
public class LongTask extends Thread {
private SharedResults sharedData;
private StringBuffer inputData;
private char target;
public LongTask(SharedResults r, StringBuffer buf, char target) {
super("Thread_" + target);
sharedData = r;
inputData = buf;
this.target = target;
}
public void run() {
int count = 0;
for (int index = 0; index
if (inputData.charAt(index) == target)
count++;
}
ResultsEntry e = new ResultsEntry(this.target, count);
sharedData.addToResults(e);
}
}



C. d. A void addToResults_method which takes the given ResultsEntry argument and adds it to the end of the shared results. This method then prints to the console the name of the current thread, the entry it added, and the shared results data structure. Handle the synchronization issue with this method. The getResult method with no arguments which returns the sum of the count entry values in the shared results data structure. Handle the synchronization issue with this method. 3. Create a class named LongTask that extends the Thread class. a. The instance (or member) private variables sharedData (of type SharedResults), inputData (of type StringBuffer), and target (of type char) A single constructor which takes the above three arguments and stores them in the instance values. Also, create a name for this thread as Thread_, Cumulative Results are [] Thread_y is adding , , ,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
