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
//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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started