
======================================
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;
}
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 14 images

- Project Overview: This project is for testing the use and understanding of stacks. In this assignment, you will be writing a program that reads in a stream of text and tests for mismatched delimiters. First, you will create a stack class that stores, in each node, a character (char), a line number (int) and a character count (int). This can either be based on a dynamic stack or a static stack from the book, modified according to these requirements. I suggest using a stack of structs that have the char, line number and character count as members, but you can do this separately if you wish.Second, your program should start reading in lines of text from the user. This reading in of lines of text (using getline) should only end when it receives the line “DONE”.While the text is being read, you will use this stack to test for matching “blocks”. That is, the text coming in will have the delimiters to bracket information into blocks, the braces {}, parentheses (), and brackets [ ]. A string…arrow_forwardOCaml Code: The goal of this project is to understand and build an interpreter for a small, OCaml-like, stackbased bytecode language. Make sure that the code compiles correctly and provide the code with the screenshot of the output. Make sure to have the following methods below: -Push integers, strings, and names on the stack -Push booleans -Pushing an error literal or unit literal will push :error: or :unit:onto the stack, respectively -Command pop removes the top value from the stack -The command add refers to integer addition. Since this is a binary operator, it consumes the toptwo values in the stack, calculates the sum and pushes the result back to the stack - Command sub refers to integer subtraction -Command mul refers to integer multiplication -Command div refers to integer division -Command rem refers to the remainder of integer division -Command neg is to calculate the negation of an integer -Command swap interchanges the top two elements in the stack, meaning that the…arrow_forwardChange the __str__ method of the Queue class (provided below) so that it prints each object in the queue along with its order in the queue (see sample output below). class Queue(): def __init__(self): self.queue = [] # implement with Python lists! # start of physical Python list == front of a queue # end of physical Python list == back of a queue def enqueue(self, new_obj): self.queue.append(new_obj); def dequeue(self): return self.queue.pop(0) def peek(self): return self.queue[0] def bad_luck(self): return self.queue[-1] def __str__(self): return str(self.queue) # let's try a more fun waySample output: >>> my_queue = Queue()>>> everyone = ["ESC", "ABC", "YOLO", "HTC"]>>> for initials in everyone:>>> my_queue.enqueue(initials)>>> print(my_queue)Output: 1: ESC2: ABC3: YOLO4: HTCarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





