Question 2 For this question, you are going to develop a merge method for an array...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
Please add code to implement the teleportation spaces!! The digits such as 1, 2, 3, etc represent magical squares. These squares will teleport you to other matching digit in the maze. For example,...
-
When a manager greets department visitors or attends ceremonies, he or she is playing the role of: Figurehead Conceptual Empowerment Productivity
-
Pan Asia Airlines was founded in 1980. Headquartered in Hong Kong, the publicly traded company has routes throughout Asia and to major airports throughout Europe and North America. While Pan Asia...
-
The world's largest carpet maker has just completed a feasibility study of what to do with the 16,000 tons of overruns, rejects, and remnants it produces every year. The company's CEO launched the...
-
Determine the \(\operatorname{load} P\) required to cause the steel \(\mathrm{W} 12 \times 50\) structural A-36 steel column to fail either by buckling or by yielding. The column is fixed at its...
-
Three students have each saved $1,000. Each has an investment opportunity in which he or she can invest up to $2,000. Here are the rates of return on the students investment projects: Harry.............
-
For each of the following composite functions, find an inner function u g(x) and an outer function y = y = f(g(x)). Then calculate dy/dx. 15. y=(3x+7) 10 17. y = sin x f(u) such that 19. y = x + 1...
-
You are a E-Commerce consultant that has been hired to assist in the development of a new online business for an existing business. The business is in Waco and sells farm material and hardware...
-
the tuition fee for maths tuition for john is expected to be 6500 per year if he attend tuition for 4 year starting from 4 years from now how much does he have to have in her account which yields 6...
-
A 25-year-old Latin, G1P1, cisfemale presents to the office with a 6-month history of amenorrhea; reports having regular menstrual cycles every month up until about 1 year ago when her menstrual...
-
A 22-year-old college football player sustains a rough hit on the field during a practice for their biggest game of the season in 2 days. The player was knocked to the ground after a collision with...
-
a) In the conventional theory of the firm, the principal objective of a business is profit maximisation. Under the differences in consumer tastes and technology drive, price and output of a given...
-
You are contemplating whether you should start your own business. You expect that the first year this new business will receive $100,000 in revenue, and incur a cost of $80,000. You will have to...
-
New procedures are put in place to impact the time it takes to hire an employee in days. Following is the data, before and after implementation, for 10 Bureaus, before and after. Do the procedures...
-
Write a while loop that uses an explicit iterator to accomplish the same thing as Exercise 7.3. Exercise 7.3. Write a for-each loop that calls the addInterest method on each BankAccount object in a...
-
Using Rayleigh's method, estimate the fundamental frequency for the lateral vibration of a uniform beam fixed at both the ends. Assume the deflection curve to be \[W(x)=c_{1}\left(1-\cos \frac{2 \pi...
-
Find the fundamental frequency of longitudinal vibration of the tapered bar shown in Fig. 8.41, using Rayleigh's method with the mode shape \[U(x)=c_{1} \sin \frac{\pi x}{2 l}\] The mass per unit...
-
Approximate the fundamental frequency of a rectangular membrane supported along all the edges by using Rayleigh's method with \[W(x, y)=c_{1} x y(x-a)(y-b)\] \[V=\frac{P}{2}...
Study smarter with the SolutionInn App