Question :
b) A Stack is a last-in first-out (LIFO) data structure. Write a Java class Stacklnt that stores a stack of integers using an alray as its private data structure. A Stacklnt has:
A maximum size, which is set when a Stacklnt object is created. An exception should be thrown if the size is outside the range 10 to . 1000
A method push, to add a value to the top of the stack. An exception should be thrown if the stack is full when push is called.
A method pop, to remove and return the value at the top of the stack. An exception should be thrown if the stack is empty when pop is called.
c) Write some example code to show how your class Stacklnt from part (b) should be used. Include examples of normal usage and for all cases when exceptions are thrown.
Ok so basically this is a question im trying to solve and would really really appreciate some help.
exceptions are in this form
// Throw an exception!
public T pop() throws EmptyStackException
{
if (contents.size() == 0)
{ throw new EmptyStackException(); }
else
{ return contents.remove(0); }
}
me so far :
public class Stack {
private int top;
private int[] storage;
Stack(int capacity) {
if (capacity <= 0)
throw new IllegalArgumentException(
"Stack's capacity must be positive");
storage = new int[capacity];
top = -1;
}
void push(int value) {
if (top == storage.length)
throw new StackException("Stack's underlying storage is overflow");
top++;
storage[top] = value;
}
int peek() {
if (top == -1)
throw new StackException("Stack is empty");
return storage[top];
}
}