Question

Show what is written by the following segment of code, given that element1, element2, and element3...

Show what is written by the following segment of code, given that element1, element2, and element3 are int variables, and queue is an object that fits the ADT of a 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)

}

Homework Answers

Answer #1

OUTPUT:

1 0 4
5
16
0
3

Explanation:

A queue is a linear data structure in which addition is done at the one end and deletion is one at another end.

Insertion can be performed at the rear end.

Deletion can be performed at the front end.

The given source code inserts the element into the queue and delete the elements from the queue and finally, the current queue is printed on the computer screen.

The statement by statement explanation of the given source code is given below:

//variable initialization
element1 = 1;
element2 = 0;
element3 = 4;

//insert the element2 = 0 into the queue
//Now the queue will be 0

queue.enqueue(element2);

//insert the element1 = 1 into the queue
//Now the queue will be 0 1

queue.enqueue(element1);

//insert the 1+4=5 into the queue
//Now the queue will be 0 1 5

queue.enqueue(element1 + element3);

//delete the first element form the queue
//Now the queue will be 1 5
//element2 = 0

element2 = queue.dequeue( );

//insert 4*4=16 into the queue
//Now the queue will be 1 5 16

queue.enqueue(element3 * element3);

//insert element2 = 0 into the queue
//Now the queue will be 1 5 16 0

queue.enqueue(element2);

//insert 3 into the queue
//Now the queue will be 1 5 16 0 3

queue.enqueue(3);

//delete the front element from the queue
//Now the queue will be 5 16 0 3
//element1 = 1

element1 = queue.dequeue( );

//display the value of element1, element2, and element3 on the computer screen
//1 0 4
System.out.println(element1 + “ “ + element2 + “ “ + element3);

//display the complete queue on the computer screen
//5
//16
//0
//3
while (!queue.isEmpty( ))
{
element1 = queue.dequeue( );
System.out.println(element1)
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
1.    Given the following segment of code: (If there is nothing output, write None.) int x;...
1.    Given the following segment of code: (If there is nothing output, write None.) int x; int y; cin >> x; cin >> y; while (x > y) {     x -= 3;     cout << x << " "; } cout << endl;        a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]               Output                                                                                                                                                                                                    x = ______________                                                                                                                                                                                                   ...
1) Explain the problem with the following code segment and suggest a way to fix it....
1) Explain the problem with the following code segment and suggest a way to fix it. int i = 42; int *p1, *p2; p1 = &i; *p2 = *p1; 2) What are the Invariants for the bag class that uses a static array?  (Hints: explain the functionality of the class’s private member variables – used and data).
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 What is the output after the code executes? Assume there are no syntax errors and the code will compile. int x = 10; do { System.out.println ( x ); x -= 3; } while (x > 0); A. 10 7 4 1...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of...
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of the matrix.
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
What is wrong with the following code segment, assumming it compiles correctly? char *a = "Hello";...
What is wrong with the following code segment, assumming it compiles correctly? char *a = "Hello"; char *b = " Mom"; while( *b != '\0' ) { *a = *b; a++; b++; }