Java program. Please just explain the code shown below and include the output. S
ID: 3716065 • Letter: J
Question
Java program. Please just explain the code shown below and include the output.
Show what is written by the following segment of code, given that item1, item2, and item3 are int variables, and queue is an object that fits our abstract description of a queue.
Assume that you can store and retrieve variables of type int in queue.
element1 = 1;
element2 = 0;
element3 = 4;
queue.enqueue(element2);
queue.enqueue(element1);
queue.enqueue(element1 + element3);
element2 = queue.dequeue();
queue.enqueue (element3*element3);
queue.enqueue(element2);
queue.enqueue(3);
element1 = queue.dequeue();
System.out.println(element1 + " " + element2 + " " + element3);
while (!queue.isEmpty())
{
element1 = queue.dequeue();
System.out.println(element1);
}
Explanation / Answer
The code is first adding the elements in the queue and then while the queue is not empty it prints each of the element in the queue
the queue after each instruction in order is given below
0
0 1
0 1 5
1 5
1 5 16
1 5 16 0
1 5 16 0 3
5 16 0 3
The output is
1 0 4
5
16
0
3?
DO give a thumbs up and in case there are doubts leave a comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.