Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create 4 new classes as are detailed in below requirements. 2. In the Results Entry class, create: private instance variables named count (int) and
Create 4 new classes as are detailed in below requirements. 2. In the Results Entry class, create: private instance variables named count (int) and target (char). A single constructor that accepts int and char values and sets them in the two instance variables. public accessor (get) methods for the two instance variables. A public toString() method that returns a String in the format . For example: 3. In the Shared Results class, create: . A private instance variable named results of type Collection , set to null. A single no-argument constructor that creates an empty ArrayList and assigns it to results. An addToResults () method that does not return a value and accepts a single Results Entry argument. It should add the Results Entry value to the end of results, and then it should print the following to the console: the name of the current thread, the entry that it added and the results data structure. Handle synchronization with this method. . A getResult() method that accepts no arguments and returns an int with the sum of all the count values from all of the Results Entry objects within the results data structure. Handle synchronization with this method. 4. In the LongTask class, which extends the java.lang.Thread class, create: private instance variables shared Data (of type Shared Results), inputData (of type StringBuffer) and target (of type char). A single constructor that accepts three arguments of types Shared Results, StringBuffer and char and assigns the values to the instance variables. It should also create a name for this thread as Thread_ . A run() method consistent with the examples in Module 6 in which you should loop through the characters in inputData and count the number of occurrences of target. After the loop has completed, instantiate Results Entry with the count and the target character passed to its constructor, and invoke the addToResults() method. 5. In the main() method of the Test class: Create an instance of StringBuffer called data. Using P01_URLDemo as an example, read the contents of the URL http://norvig.com/big.txt. After reading each line from the network resource, convert it to lowercase and append it to data. This will be the input data that is shared for each thread. Create a Shared Results instance and assign it to a variable called shared Results. Create a char variable called letter, with its initial value set to 'a'. Create a LongTask[] array of size 26 and then loop through it. Each time through the loop, create a new LongTask object (by passing shared Results, data and letter to LongTask's constructor) and then start a corresponding thread. The last statement within the loop should be letter++; This will cause the current character ('a', 'b', 'c', etc.) to be replaced with the next one alphabetically. Wait for all of the threads to complete using each thread's join() method. Print the total count stored in Shared Results by calling its getResult() method. For example: Character Count = 5079398 Sample output is shown on the next page. Sample Output Different runs of the program will produce the output in different sequences, but the final result will be the same. Input Data length: 6488666 Thread Thread a running Thread Thread_b running Thread Thread_c running Thread Thread_d running Thread Thread_e running Thread Thread_f running Thread Thread_g running Thread Thread_h running Thread Thread_1 running Thread Thread running Thread Thread_k running Thread Thread_1 running Thread Thread_m running Thread Thread_n running Thread Thread_o running Thread Thread p running Thread Thread_q running Thread Thread_r running Thread Thread_s running Thread Thread_t running Thread Thread_u running Thread Thread_v running Thread Thread_w running Thread Thread x running Thread Thread_y running Thread Thread_z running Cumulative Results are [ ] Thread_k is adding , Thread 1 is adding , Cumulative Results are [ , ] Thread g is adding , Cumulative Results are [ , , , , ] Thread_d is adding , Cumulative Results are [ , , , , ] Thread_a is adding , Cumulative Results are [ , , , , , ] Thread_m is adding , Cumulative Results are [ , , , , , 408089>, ] Thread e is adding , Cumulative Results are [ , , , , , , , ] Thread_z is adding , Cumulative Results are [ , , , , , 408089>, , , ]
Step by Step Solution
★★★★★
3.51 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
Q 1 class named ResultsEntry class ResultsEntry private members private int count private ...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