Why doesnt it compile ?
import java.util.*;
class MyQueue {
private final Stack stack1;
private final Stack stack2;
public MyQueue() {
stack1 = new Stack();
stack2 = new Stack();
}
public void push(int x) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
stack1.push(x);
while (!stack2.empty()) {
stack1.push(stack2.pop());
}
}
public int pop() {
return stack1.empty() ? -1 : stack1.pop();
}
public int peek() {
return stack1.empty() ? -1 : stack1.peek();
}
public boolean empty() {
return stack1.empty();
}
}
Stacktrace:
MyQueue.java:14: error: cannot find symbol
while (!stack1.empty()) {
^
symbol: method empty()
location: variable stack1 of type Stack
MyQueue.java:18: error: cannot find symbol
while (!stack2.empty()) {
^
symbol: method empty()
location: variable stack2 of type Stack
MyQueue.java:24: error: cannot find symbol
return stack1.empty() ? -1 : stack1.pop();
^
symbol: method empty()
location: variable stack1 of type Stack
MyQueue.java:28: error: cannot find symbol
return stack1.empty() ? -1 : stack1.peek();
^
symbol: method empty()
location: variable stack1 of type Stack
MyQueue.java:28: error: cannot find symbol
return stack1.empty() ? -1 : stack1.peek();
^
symbol: method peek()
location: variable stack1 of type Stack
MyQueue.java:32: error: cannot find symbol
return stack1.empty();
^
symbol: method empty()
location: variable stack1 of type Stack
6 errors
Course: https://www.educative.io/collection/10370001/4651429556125696
Lesson: https://www.educative.io/collection/page/10370001/4651429556125696/5184292218732544