Question
I need help with number 4. number 1,2 and 3 has been sorted.thanks 1.Create a class named ResultsEntry containing the following: Private instance variables named
I need help with number 4. number 1,2 and 3 has been sorted.thanks
1.Create a class named ResultsEntry containing the following:
-
Private instance variables named count (int) and target (char)
-
A single constructor with the two values
-
Public get methods for the two variables
-
A public toString() method that returns a string in the format target,count
2. Create a class named SharedResults containing the following:
-
Private instance variable named results of ArrayList
type -
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.
-
3. Create a class named LongTask that extends the Thread class containing the following:
-
Private instance variables sharedData (of type SharedResults), inputData (of type StringBuffer) and target (of type char)
-
Constructor that takes three arguments and stores them in the instance variables. It should also create a name for this thread as Thread_
. -
In the run() method, use a loop over the characters in inputData and count the number of occurrences of target. After the loop is done, create a ResultsEntry object with this count and the target character, and invoke the addToResults() method of results.
4. Create a Test class to test the following functionality in its main() method:
-
Using P01_URLDemo as an example, read the contents of the URL http://norvig.com/big.txt into a StringBuffer instance. After reading each line from the network resource, convert it to lowercase and append it to the StringBuffer. This will be the input data that is shared for each thread.
-
4.Create a SharedResults instance and assign it to a variable.
-
Create 26 LongTask objects using an array of size 26. Each LongTask object is responsible for
counting the occurrences of the characters from 'a' to 'z'. Start a corresponding thread after
creating each one.
-
Wait for all the threads to complete using the join() method.
-
Print the value of result.
JAva
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