Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago