Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 2 For this question, you are going to develop a merge method for an array of String queues, as shown in the code
Question 2 For this question, you are going to develop a merge method for an array of String queues, as shown in the code snippet below: public class Queue Utils { public static Queue merge (Queue [] array) { if (array == null || array.length } == 0) { throw new IllegalArgumentException(); // All the code that you write for Question 2 goes here! } } The merge(...) method should combine together all the input queues (i.e., array[0], ar- ray[1],..., array [array.length-1]). The main characteristic of the combination is fairness to all the queues. Imagine that each queue (i.e., each array[i]) is a queue of customers waiting for some service, and that you have only a single service point. To make sure that all queues receive fair service, we are going to offer service as follows: we serve the front element of the first queue, then the front element of the second queue, then the front element of the third queue and so on. When the front elements of all the queues have been served, we do another iteration, now serving the second elements across all the queues. This will continue until all the elements in all queues have been served. Your implementation of merge (...) should mimic this interleaving behaviour by building a single queue out of all the given queues. eistive Note that the queues do not necessarily have an equal number of elements. Your merge should account for all elements in all the queues even when the queues have different sizes. The merge i best illustrated with the simple examples provided in "Example Output for Question 2". Accdez 3 You need and are thus permitted to define instance variables in the iterator class, i.e., MyLinkedListIterator. 4java.util.NoSuchElementException April 2022 ITI 1121 Page 7 of 8 IMPORTANT! All array[0], array[1], ..., array[array.length-1] will be empty, once the ex- ecution of merge (...) is completed. You are not required to restore the queues in array[0], array[1], ..., array[array.length-1] to their original state. Question 2 does not require any exception handling beyond what is provided to you in the template code. Example Output for Question 2 To test your implementation of merge(...), you can use the Q2Test.java in the template code provided to you. The output from running Q2Test should be as follows: $ javac Q2Test.java $ java Q2Test ===== === Testing Q2 == >>>> Testing with two queues >>>>> Queue: [A1, A2, A3, A4, A5, A6, A7, A8] Queuel: [B1, B2, B3, B4] Merged queue: [A1, B1, A2, B2, A3, B3, A4, B4, A5, A6, A7, A8] >>>> Testing with five queues >>>>> Queue: [A1, A2, A3, A4, A5] Queuel: [B1, B2, B3] Queue2: [C1] Activer Accdez a Queue3: [] Queue4: [E1, E2, E3, E4, E5, E6, E7] Merged queue: [A1, B1, C1, 1, A2, B2, E2, A3, B3, E3, A4, E4, A5, E5, E6, E7] $ Important Restrictions for Question 2 You cannot change LinkedQueue.java or Queue.java. All your variables should be local variables. You cannot declare any class or instance variables in QueueUtils (or anywhere else for that matter). The local reference variables in merge(...) as well as in any helper private methods that you may implement can only be of type Queue . You are allowed to define local primitive variables, e.g., a boolean local variable.
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