
import java.util.*;
public class StackLimitada{
    static final int MAX=50;

    private Object[] stack;
    private int n;
    

    public StackLimitada(int n){
	if (n>0 && n<MAX )
	    stack=new Object[n];
	else
	    stack=new Object[MAX];
	n=0;
    }

    public boolean empty(){
	return n==0;
    }
    
    public void push(Object o) throws IndexOutOfBoundsException {
	if (n==stack.length-1)
	    throw new IndexOutOfBoundsException();
	else
	    stack[n++]=o;
    }
    
    public Object peek() throws EmptyStackException {
	if (empty())
	    throw new EmptyStackException();
	else
	    return stack[n-1];
    }
    
    public void pop() throws EmptyStackException{
	if (empty())
	    throw  new EmptyStackException();
	else
	    n--;
    }

    public String toString(){
	String s="[";
	for (int i=n-1; i>-1;i--)
	    s+=stack[i].toString()+ ((i>0)?";":"");
	return s+"]";
    }
	
    public static void main(String args[]){
	int d=Consola.leInt("Qual a dimensao da Stack");
	StackLimitada s=new StackLimitada(d);
	try{
	    //s.peek();
	    s.push(new Integer(23));
	    s.push(new Integer(24));
	    System.out.println(s);
	    s.push(new Integer(100));
	    s.push(new Integer(101));
	    System.out.println(s);
	    s.pop();
	    System.out.println(s);
	}
	catch (EmptyStackException e){
	    System.out.println("STACK VAZIA!!!!");
	}
	
	catch ( IndexOutOfBoundsException e){
	    System.out.println("STACK CHEIA !!!!!"); 
	    
	}
	
	boolean f=Consola.leBool("Digite um booleano:");
	System.out.println("foi lido "+ f);
    }
}


