// ExcDemo.java

class Stack
{
   private int size, top;
   private int [] values;

   class Parent extends Exception
   {
      int index, value;

      Parent (int index, int value)
      {
         this.index = index;
         this.value = value;
      }

      int getIndex ()
      {
              return index;
      }

      int getValue ()
      {
              return value;
      }
   }

   class Empty extends Parent
   {
      Empty (int index)
      {
         super (index, 0);
      }
   }

   class Full extends Parent
   {
      Full (int index, int value)
      {
         super (index, value);
      }
   }

   Stack (int size)
   {
      this.size = size;         // Maximum number of integer values in stack.
      top = -1;                 // Stack defaults to empty.
      values = new int [size];  // Create stack to hold size integer values.
   }

   int isEmpty ()
   {
      return (top == -1) ? 1 : 0;
   }

   void push (int value) throws Full
   {
      if (top >= size-1)        // When top equals size-1, the stack is full.
         throw new Full (top, value); // When full, throw Stack::Full
                                      // exception.

      values [++top] = value;
   }

   int pop () throws Empty
   {
      if (top < 0)              // When top less than 0, the stack is empty.
          throw new Empty (top);    // When empty, throw Stack::Empty
                                    // exception.

      return values [top--];
   }
};

class ExcDemo
{
   public static void main (String [] args)
   {
      // Create a stack that holds 5 integer values (maximum).

      Stack s = new Stack (5);

      System.out.println ("Stack object created. Stack holds a maximum" +
                          " of five integer values.");

      int values [] = { 52, -32, 468, 345, 42, 91 };

      try
      {
          // Attempt to push 6 integer values onto the stack. The last
          // push attempt results in a Stack.Full exception.

          for (int i = 0; i < 6; i++)
          {
              System.out.println ("Attempting to push integer value " +
                                  values [i] + ".");
              s.push (values [i]);
          }
      }
      catch (Stack.Full f)
      {
          System.out.println ("Stack is full. Could not push integer" +
                              " value " + f.getValue () +  ". Top" +
                              " index equals " + f.getIndex () + ".");
      }

      try
      {
          // Attempt to pop 6 values from the stack. The last pop
          // attempt results in a Stack::Empty exception.

          for (int i = 0; i < 6; i++)
              System.out.println ("Popping integer value " + s.pop () +
                                  ".");
      }
      catch (Stack.Empty e)
      {
          System.out.println ("Stack is empty. Could not pop integer" +
                              " value. Top index equals " +
                              e.getIndex () + ".");
      }
   }
}