Stack Interface

/*
Design a Java interface for ADT Stack. Implement this interface using array. Provide necessary exception handling in both the implementations.
*/

 

// Stack Interface
 
import java.util.*;
 
interface stk_intf
{
     public int isEmpty();
     public int isFull();
     public void push(int v);
     public void pop();
}
 
class Stk implements stk_intf
{
     int [] stack = new int[5];
     int top;
    
     Stk()
     {
          top=-1;
     }
    
     public int isEmpty()
     {
          if(top==-1)
              return 1;
          return 0;
     }
    
     public int isFull()
     {
          if(top>=4)
              return 1;
          return 0;
     }
    
     public void push(int v)
     {
          if(isFull()==1)
              System.out.println(“Error – stack full.”);
          else
          {
              top++;
              stack[top]=v;
             
              this.display();
          }
     }
    
     public void pop()
     {
          if(isEmpty()==1)
              System.out.println(“Error – stack underflow.”);
          else
          {  
              top–;
              this.display();
          }
     }
    
     public void display()
     {  
          System.out.println(“\n Stack Elements:”);
          for(int i=top;i>=0;i–)
              System.out.print(stack[i]+”  “);
     }
 
}
 
class Stk_pgm
{
     public static void main(String as[])
     {
          Scanner s = new Scanner(System.in);
          Stk s1 = new Stk();
         
          while(true)
          {
              System.out.println(“\n1 – push \t 2 – pop \t 3 – exit”);
             
              int ch;
              System.out.print(“Enter choice : “);   
              ch = s.nextInt();
             
              if(ch==1)
              {
                   System.out.print(“Enter data : “);
                   int val = s.nextInt();
                  
                   s1.push(val);
              }
               else if(ch==2)
                   s1.pop();
              else if(ch==3)
                   System.exit(0);
              else
                   System.out.println(“Invalid choice”);
             
          }       
     }
}