Create a generic class, MyQueue, with a public Stack fields s1 and s2. Add metho
ID: 3769743 • Letter: C
Question
Create a generic class, MyQueue, with a public Stack fields s1 and s2. Add methods for enqueue, dequeue, and isEmpty. Create a default constructor that initializes the empty stacks. In the enqueue method, complete these three steps: 1. Pop every element from s1 and push it into s2. 2. Push the enqueue parameter element into s1. 3. Pop every element from s2 and push it into s1. In the dequeue method, simply pop from s1 and return that element. In the isEmpty method, return true only if s1 is empty, otherwise return false.
Explanation / Answer
public class StackMachine extends IntStack {
/**
* Pop the two top integers from the stack, add
* them, and push their integer sum.
* @throws EmptyStack if stack runs out
*/
public void add() throws EmptyStack {
int i = pop();
int j = pop();
push(i + j);
}
/**
* Pop the two top integers from the stack, divide
* them, and push their integer quotient.
* @throws EmptyStack if stack runs out
*/
public void divide() throws EmptyStack {
int i = pop();
int j = pop();
push(i / j);
}
/**
* Pop the two top integers from the stack, multiply
* them, and push their integer product.
* @throws EmptyStack if stack runs out
*/
public void multiply() throws EmptyStack {
int i = pop();
int j = pop();
push(i * j);
}
/**
* Pop the two top integers from the stack, subtract
* the second from the first, and push their integer
* difference.
* @throws EmptyStack if stack runs out
*/
public void subtract() throws EmptyStack {
int i = pop();
int j = pop();
push(i - j);
}
}
/**
* An IntStack is an object that holds a stack of ints.
*/
public class IntStack {
private Node top = null; // The top Node in the stack
/**
* Test whether this stack has more elements.
* @return true if this stack is not empty
*/
public boolean hasMore() {
return (top != null);
}
/**
* Pop the top int from this stack and return it.
* @return the popped int
* @throws EmptyStack if stack is empty
*/
public int pop() throws EmptyStack {
Node n = top;
if (n == null) throw new EmptyStack();
top = n.getLink();
return n.getData();
}
/**
* Push an int on top of this stack.
* @param data the String to add
*/
public void push(int data) {
top = new Node(data,top);
}
}
/**
* A Node is an object that holds an int and a link
* to the next Node. It can be used to build linked
* lists of ints.
*/
public class Node {
private int data; // Each node has an int...
private Node link; // ...and a link to the next Node
/**
* Node constructor.
* @param theData the int to store in this Node
* @param theLink a link to the next Node
*/
public Node(int theData, Node theLink) {
data = theData;
link = theLink;
}
/**
* Accessor for the int data stored in this Node.
* @return our int item
*/
public int getData() {
return data;
}
/**
* Accessor for the link to the next Node.
* @return the next Node
*/
public Node getLink() {
return link;
}
}
public class EmptyStack extends Exception {
public EmptyStack() {
super("Trying to pop an empty stack.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.