Question

Revision Question Consider the following Java class: { public static void main (String [ ] args)...

Revision Question

Consider the following Java class:

{

public static void main (String [ ] args)

{

ArrayQueue<Integer> queue;

queue = new ArrayQueue<Integer> () ;

Integer x, y ;

x = 3;

y = 6;

queue.offer (x) ;

queue.offer (12) ;

queue.offer (y) ;

y = queue.peek () ;

queue.poll () ;

queue. offer (x - 2) ;

queue.offer (x) ;

queue.offer (y + 4) ;

System.out.println ("Queue Elements: ") ;

while (! queue.empty() )

System.out.print (queue.poll () + " " ) ;

}

}

In the above, assume that the queue interface is defined as follows:

public interface Queue<E>

{

public boolean empty () ;

public int size () ;

public boolean offer (E x ) ;

public E peek () ;

public E poll () ;

}

and that the implementation ArrayQueue <E> is as discussed in the lectures.

What output would be produced when the main method of the TestQueue class is executed? If you think that no output is produced, write None.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

OUTPUT:
Queue Elements:
12 6 1 3 7

EXPLANATION THROUGH COMMENTS:

public static void main(String[] args)
{
    ArrayQueue<Integer> queue;
    queue = new ArrayQueue<Integer> () ;
    Integer x, y ;
    x = 3;
    y = 6;
    queue.offer (x) ;//Inserts x = 3 into the queue. Current queue = [3]
    queue.offer (12) ;//Inserts 12 into the queue. Current queue = [3 12]
    queue.offer (y) ;//Inserts y = 6 into the queue. Current queue = [3 12 6]
    y = queue.peek () ;//Element at front of the queue is returned i.e. 3. So, y = 3. Current queue = [3 12 6]
    queue.poll () ;//Deletes and returns element at front of the queue which is 3. Current queue = [12 6]
    queue. offer (x - 2) ;//Inserts x-2 = 3-2 = 1 into the queue. Current queue = [12 6 1]
    queue.offer (x) ;//Inserts x = 3 into the queue. Current queue = [12 6 1 3]
    queue.offer (y + 4) ;//Inserts y+4 = 3+4 = 7 into the queue. Current queue = [12 6 1 3 7]
    System.out.println ("Queue Elements: ") ;
    while (! queue.empty() )//Loop till queue is not empty
        System.out.print (queue.poll () + " " ) ;//This will print 12 6 1 3 7 as poll will keep on deleting
                                                 // front elements and will return the deleted element
}
Add a comment
Know the answer?
Add Answer to:
Revision Question Consider the following Java class: { public static void main (String [ ] args)...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • Question 28 public class Main public static void main(String args[]) { Iparentp=new Child (1.2); p.printo: interface...

    Question 28 public class Main public static void main(String args[]) { Iparentp=new Child (1.2); p.printo: interface Iparent public int x = 1; public void printo: class Child implements Iparent private int y public Child(int a, int b) y b: public void printot System.out.print("Child"-yl: Child 2 public int x = 1; public void print(); class Child implements Iparent! private int y public Child(int a, in lb) y=b; public vold print! System.out.pnt("Child"+y): Child 2 1 None of the above • Previous

  • What is the output of following program: public class Test{ public static void main(String[] args) {...

    What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus";...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT