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
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).
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...
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...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
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 >=...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark them in the code and explain the reason briefly in place. #include<iostream> using namespace std; int main() { cout<<”Please input the radius (in integer): ”>>endl; int 1var = 10, var=20; double area = 40; cin<< var; //get an input of the radius and store in var float continue; float const pi = 2.64; pi += 0.5; do { cout<<”The radius is ”<<”var”<<endl; //print the...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Second question Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT