Question
When testing no junit please ! You have to design and implement a collection of classes that process a collection of String objects. Processing a
When testing no junit please !
You have to design and implement a collection of classes that process a collection of String objects. Processing a string means computing an integer value as below.
int value = 0; for (int index = 0; index < string.length(); index++) {
value = value + string.charAt(index) - 'a'; }
There is nothing special about the above processing approach. I am stating it, so there is uniformity in your approaches.
The strings are stored in a queue by a driver. In particular, the following code must compile error free and execute correctly on your implementation. You must implement your own generic Queue class and not borrow it from anywhere else, including the Java libraries.
public static void main(String[] args) { Queue
int length = ((int) (Math.random() * 10000)) % 100; StringBuffer buffer = new StringBuffer(); for (int count = 0; count < length; count++) {
buffer.append((char) ((((int) (Math.random() * 10000)) % 26) + 'a')); }
String newString = new String(buffer);
collection.enqueue(newString); }
RequestProcessor processor = new RequestProcessor(collection); int numberOfThreads = 5; Thread[] threads = new Thread[numberOfThreads]; for (int index = 0; index < threads.length; index++) {
threads[index] = new Thread(processor);
threads[index].start(); }
try {
1
for (int index = 0; index < threads.length; index++) {
threads[index].join(); }
} catch(InterruptedException ie) {
ie.printStackTrace(); }
ThreadStatisticsSetup.print(); }
One possible output is
Count 1700 average length 49 Count 2603 average length 49 Count 2212 average length 49 Count 1744 average length 49 Count 1741 average length 50
The program was run on 5 threads. There are 10000 strings. The number of strings and the average length of strings processed by each thread (thread statistics) are printed by the class ThreadStatisticsSetup.
Each thread in the RequestProcessor class must use a lock before accessing the collection. The threads must be as concurrent as possible, within reason. Each thread must have its own private area to store the average length and count of strings processed by it. This private area must be supplied by ThreadStatisticsSetup. Moreover, these private areas must be stored in a queue by ThreadStatisticsSetup, so that class can easily print the queue in response to the call ThreadStatisticsSetup.print().
The generic queue can be used both for the collection of strings and the collection of private areas for storing thread statistics.
Solution Steps
Implement the generic Queue class with the method enqueuer, dequeue, and print. Declare the Node class within the Queue class.
Create the ThreadStatistics class to store the count of strings processed and the average length with the needed getters, setters, and override the toString() method.
Implement the RequestProcessor class to extend Thread. After processing a String object, use the ThreadStatistics class to update the average length and count of strings processed.
Create the ThreadStatisticsSetup to supply a ThreadStatistics object for each thread. This object should be stored in a Queue object (The Queue class was created in Step 1.)
Put the main method in a Driver class and test your program.
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