Exercise 3
Start by creating a new sub package named stack
inside your package YourLnuUserName_assign4 and save all .java files related to this
exercise inside this package.
A stack is a LiFo (Last-in, first-out) data structure with three basic operations: push, pop and peek.
push is putting an element on the top of the stack, pop removes (and returns) the top element, and
peek returns (without removing) the top element. Think of a stack as a pile of plates that can be
found in certain restaurants. You can only add and remove the top-most plate. You can not remove
any plates in the middle of the pile (without first removing all plates above it).
Your task is to implement the following stack interface:
public interface Stack {
int size(); // Current stack size
boolean isEmpty(); // true if stack is empty
void push(Object element); // Add element at top of stack
Object pop(); // Return and remove top element,
// exception if stack is empty
Object peek(); // Return (without removing) top element,
// exception if stack is empty.
Iterator<Object> iterator(); // Element iterator
}
The iterator traverses all elements currently in the stack, starting with the top element.
Illegal operations on an empty stack (e.g., pop() and peek()) should generate an exception.
You should also present a test program StackMain.java that demonstrates how each method can be used.
Notice: You are not allowed to use any of the data structures in the Java library. However, you can
use arrays.