Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. Show w Show what is written by the following segments of code, given that elem

ID: 3690856 • Letter: #

Question

. Show w Show what is written by the following segments of code, given that elementl. element2, a abs ent2, and element3 are int variables, and queue is an object that fits the tract description of a queue as given in Section 5.2. Assume that you can and retrieve values of type int in queue. store and a. elementl 1 element2 -0 element3 -4 queue.enqueue (element2); queue.enqueue (elementl): queue.enqueue(elementl + element3): queue.enqueue(elementl element3): element2 queue.dequeue O queue.enqueue (element3'element3): queue.enqueue (element2): queue.enqueue (3): elementlqueue.dequeueO: System.out.println(elementl+"+element2+ element3): while (!queue.isEmpty)) elementl queue.dequeueO: System.out.println (elementl): b. elementl -4: element3 -0 element2 elementi 1: queue.enqueue (element2): queue. enqueue (element2 1): queue.enqueue (elementl): element2 queue . dequeue ( ); element 1 = element2 + 1; queue.enqueue (element1): queue.enqueue (element3): while (!queue.IsEmpty))

Explanation / Answer

Here is the expected output, explained in comments for you:

element1 = 1;   //1 will be stored in element1
element2 = 0;   //0 will be stored in element2
element3 = 4;   //4 will be stored in element3
queue.enqueue(element2);   //The value in element 2, i.e., 0 will be enqueued to the queue. So, the elements in the queue are: 0.
queue.enqueue(element1);   //The value in element 1, i.e., 1 will be enqueued to the queue. So, the elements in the queue are: 0 1.
queue.enqueue(element1 + element3);   //The sum of element1, and element 3 i.e, 1 + 4 = 5 will be enqueued to the queue. So, the elements in the queue are: 0 1 5.
element2 = queue.dequeue();   //The value in the front of the queue, i.e., 0 will be dequeued, and stored into element2. So, the elements in the queue are: 1 5.
queue.enqueue(element3 * element3); //The product of element 3 with itself, i.e., 4 * 4 = 16 will be enqueued to the queue. So, the elements in the queue are: 1 5 16.
queue.enqueue(element2);   //The value in element2 i.e., 0 will be enqueued to the queue. So, the elements in the queue are: 1 5 16 0.
queue.enqueue(3);           //The value 3 will be enqueued to the queue. So, the elements in the queue are: 1 5 16 0 3.
element1 = queue.dequeue();   //The value in the front of the queue, i.e., 1 will be dequeued, and stored into element1. So, the elements in the queue are: 5 16 0 3.
System.out.println(element1 + " " + element2 + " " + element3);   //element1, element2, and element3 are printed to the screen. So, output is: 1 0 4 will be printed.
while(!queue.isEmpty())   //As long as queue is not empty,
{
element1 = queue.dequeue();   //dequeue the element and store it in element1.
System.out.println(element1);   //Print the value in element1.
}
//So, the values 5 16 0 3 will be printed by the while loop.
//The values on the screen at the end of this whole block is: 1 0 4 5 16 0 3.


element1 = 4;   //4 will be stored in element1.
element3 = 0;   //0 will be stored in element3.
element2 = element1 + 1;   //element1 + 1, i.e., 4 + 1 = 5 will be stored in element2.
queue.enqueue(element2);   //The value in element2 i.e., 5 will be enqueued to the queue. So, the elements in the queue are: 5.
queue.enqueue(element2 + 1);//The value in element2 + 1 = 5 + 1 = 6 will be enqueued to the queue. So, the elmenets in the queue are: 5 6.   
queue.enqueue(element1);   //The value in element1 i.e., 4 will be enqueued to the queue. So, the elements in the queue are: 5 6 4.
element2 = queue.dequeue();   //The value in the front of the queue, i.e., 5 will be dequeued, and stored into element2. So, the elements in the queue are: 6 4.
element1 = element2 + 1;   //The value in element2 + 1 = 5 + 1 = 6 will be stored into the variable element1.
queue.enqueue(element1);   //The value in element1 i.e., 6 will be enqueued to the queue. So, the elements in the queue are: 6 4 6.
queue.enqueue(element3);   //The value in element3 i.e., 0 will be enqueued to the queue. So, the elements in the queue are: 6 4 6 0.
while(!queue.IsEmpty())       //As long as queue is not empty,
{
element1 = queue.dequeue();   //dequeue the element and store it in element1.
System.out.println(element1);   //Print the value in element1.
}   
//So, the values 6 4 6 0 will be printed by the while loop, and by the time we come out of the loop, the value 0 will be stored in element1.
System.out.println(element1 + " " + element2 + " " + element3); //element1, element2, element3 are printed to the screen. So, output is: 0 5 0 will be printed.
//The values on the screen at the end of this whole block is: 6 4 6 0 0 5 0.