idastan97

CSCI152 L6 P1

Feb 1st, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////// IntrStack ADT (interface)
  2. public interface IntStack {
  3.  
  4.     /**
  5.      * Puts the given element on the top of the stack.
  6.      *
  7.      * @param value element to be added on the top of the stack
  8.      */
  9.     public void push(int value);
  10.    
  11.     /**
  12.      * Removes and returns the top most element of the stack
  13.      *
  14.      * @return the top most element of the stack
  15.      * @throws Exception if the stack is empty
  16.      */
  17.     public int pop() throws Exception;
  18.        
  19.     /**
  20.      * @return the size of the stack
  21.      */
  22.     public int getSize();
  23.    
  24.     /**
  25.      * Removes all elements from the stack
  26.      */
  27.     public void clear();
  28.        
  29.     /**
  30.      * @return a String representation of the stack
  31.      */
  32.     @Override
  33.     public String toString();    
  34.  
  35. }
  36.  
  37. ///////////////////////////////////////////////////////////////////////////////// ArrayIntStack Implementation
  38. public class ArrayIntStack implements IntStack{
  39.    
  40.     private int[] values;
  41.     private int size;
  42.    
  43.     public ArrayIntStack(){
  44.         values=new int[10];
  45.         size=0;
  46.     }
  47.    
  48.     @Override
  49.     public void push(int value) {
  50.         if (size==values.length){
  51.             int[] tmpArr=new int[2*values.length];
  52.             for (int i=0; i<values.length; i++)
  53.                 tmpArr[i]=values[i];
  54.             values=tmpArr;
  55.         }
  56.         values[size++]=value;
  57.     }
  58.  
  59.     @Override
  60.     public int pop() throws Exception {
  61.         if (size<=0){
  62.             throw new Exception("The stack is empty.");
  63.         }
  64.         return values[(size--)-1];
  65.     }
  66.  
  67.     @Override
  68.     public int getSize() {
  69.         return size;
  70.     }
  71.  
  72.     @Override
  73.     public void clear() {
  74.         values=new int[10];
  75.         size=0;
  76.     }
  77.    
  78.     @Override
  79.     public String toString(){
  80.         String list="bottom[";
  81.         for (int i=0; i<size; i++){
  82.             list+=values[i];
  83.             if (i<size-1)
  84.             list+=", ";
  85.         }
  86.         return list+"]top";
  87.     }
  88. }
  89.  
  90. /////////////////////////////////////////////////////////////////////////////////// Test Classs
  91. public class testClass {
  92.     public static void main(String[] args){
  93.         IntStack myStack=new ArrayIntStack();
  94.         try{
  95.             System.out.println(myStack.pop()+"\n");
  96.         } catch (Exception ex){
  97.             System.out.println(ex.getMessage()+"\n");
  98.         }
  99.        
  100.         for (int i=0; i<12; i++){
  101.             myStack.push(i);
  102.         }
  103.         System.out.println(myStack);
  104.         System.out.println("Size: "+myStack.getSize()+"\n");
  105.        
  106.         try {
  107.             System.out.println("Elements removed: "+ myStack.pop()+" "+
  108.                 myStack.pop());
  109.         } catch (Exception ex){
  110.             System.out.println(ex.getMessage());
  111.         }
  112.         System.out.println(myStack);
  113.         System.out.println("Size: "+myStack.getSize()+"\n");
  114.        
  115.         myStack.clear();
  116.         System.out.println(myStack);
  117.         System.out.println("Size: "+myStack.getSize()+"\n");
  118.        
  119.         for (int i=0; i<22; i++){
  120.             myStack.push(i);
  121.         }
  122.         System.out.println(myStack);
  123.         System.out.println("Size: "+myStack.getSize()+"\n");
  124.        
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment