Question: Create a class named SharedResults containing the following: Private instance variable named results of ArrayList . Default constructor that initializes results. void addToResults() method that

Create a class named SharedResults containing the following:

Private instance variable named results of ArrayList.

Default constructor that initializes results.

void addToResults() method that takes a ResultsEntry argument and adds it to the end of results, then prints to the console the name of the current thread, the entry it added and the results data structure. Handle synchronization with this method

A getResult() method with no arguments that returns the sum of the count entry values in results data 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) ;

}

I didn't know how to do the sum at the end? and I am not sure if the code inside the method addToResults() is correct in my code?

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!