- Write Java code. Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1;  private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public int size() public void insert(String value) public String remove() private void shiftStacks() public boolean isEmpty() public boolean isFull() Test case1: Testing empty queue - Input1: 1 - Output1: [0:Empty:] Test case2: Testing insert method - Input2: 2 4 a b c d - Output2: [4:Full:a, b, c, d] Test case3: Testing remove method - Input3: 3 4 a b c d - Output3: [3:b, c, d] *MyQueue.java: import java.util.*; import java.lang.*; import java.io.*;   public class MyQueue { private int maxCapacity = 4; private Stack stack1; private Stack stack2;   public MyQueue() { } public int size() { } public void insert (String value) { } public String remove() { } private void shiftStacks() { } public boolean isEmpty() { } public boolean isFull() { }   @Override public String toString () { shiftStacks(); StringBuilder sb = new StringBuilder("["); sb.append(this.size()).append(":"); if(this.isEmpty()) sb.append("Empty").append(":"); else if (this.isFull()) sb.append("Full").append(":"); while(!isEmpty()){ sb.append(this.remove()); if(this.size()!=0) sb.append(", "); } sb.append("]"); return sb.toString(); } } *MyQueueDriver.java (Don't change anything in the code below): import java.util.*; import java.lang.*; import java.io.*;   public class MyQueueDriver { public static void main(String[] args) { MyQueue q = new MyQueue(); Scanner input = new Scanner(System.in); int which = input.nextInt(); int quantity = 0; if(which != 1) quantity = input.nextInt(); String[] elements = new String[quanity]; for(int i = 0; i < quantity; i++)      elements[i] = input.next(); switch (which) { case 1 :       System.out.println(q.toString());       break; case 2 :        for(String s : elements)             q.insert(s); System.out.println(q.toString()); break; case 3 :         for(String s : elements)              q.insert(s); q.remove(); System.out.println(q.toString()); break; } } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question

======================================

Answer question below:

- Write Java code.

Implement a MyQueue class which implements a queue using two stacks.

private int maxCapacity = 4;

private Stack<String> stack1; 

private Stack<String> stack2;

Note:

  • You can use library Stack but you are not allowed to use library Queue and any of its methods
  • Your Queue should not accept null or empty String or space as an input

You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well:

public int size()

public void insert(String value)

public String remove()

private void shiftStacks()

public boolean isEmpty()

public boolean isFull()

Test case1: Testing empty queue

- Input1:

1

- Output1:

[0:Empty:]

Test case2: Testing insert method

- Input2:

2

4

a

b

c

d

- Output2:

[4:Full:a, b, c, d]

Test case3: Testing remove method

- Input3:

3

4

a

b

c

d

- Output3:

[3:b, c, d]

*MyQueue.java:

import java.util.*;

import java.lang.*;

import java.io.*;

 

public class MyQueue {

private int maxCapacity = 4;

private Stack<String> stack1;

private Stack<String> stack2;

 

public MyQueue() { }

public int size() { }

public void insert (String value) { }

public String remove() { }

private void shiftStacks() { }

public boolean isEmpty() { }

public boolean isFull() { }

 

@Override

public String toString () {

shiftStacks();

StringBuilder sb = new StringBuilder("[");

sb.append(this.size()).append(":");

if(this.isEmpty())

sb.append("Empty").append(":");

else if (this.isFull())

sb.append("Full").append(":");

while(!isEmpty()){

sb.append(this.remove());

if(this.size()!=0) sb.append(", ");

}

sb.append("]");

return sb.toString();

}

}

*MyQueueDriver.java (Don't change anything in the code below):

import java.util.*;

import java.lang.*;

import java.io.*;

 

public class MyQueueDriver {

public static void main(String[] args) {

MyQueue q = new MyQueue();

Scanner input = new Scanner(System.in);

int which = input.nextInt();

int quantity = 0;

if(which != 1) quantity = input.nextInt();

String[] elements = new String[quanity];

for(int i = 0; i < quantity; i++)

     elements[i] = input.next();

switch (which) {

case 1 :

      System.out.println(q.toString());

      break;

case 2 :

       for(String s : elements)

            q.insert(s);

System.out.println(q.toString());

break;

case 3 :

        for(String s : elements)

             q.insert(s);

q.remove();

System.out.println(q.toString());

break;

}

}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 14 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning