Advertisement
Guest User

Hussi_code

a guest
Apr 28th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. package Upp1;
  2. import java.util.*;
  3.  
  4. // ArrayStack class
  5. // The Stack class represents a last-in-first-out (LIFO) stack of objects.
  6. // CONSTRUCTION: with no initializer
  7. //
  8. // ******************PUBLIC OPERATIONS*********************
  9. // void push( x )         --> Insert            --> Remove most recently inserted item
  10. // AnyType peek( )         --> Return most recently inserted item
  11. // AnyType topAndPop( )   --> Return and remove most recent item
  12. // boolean isEmpty( )     --> Return true if empty; else false
  13. // void makeEmpty( )      --> Remove all items
  14. // ******************ERRORS********************************
  15. // top, pop, or topAndPop on empty stack
  16.  
  17. public class ArrayStack<AnyType>
  18.  
  19. {
  20.    
  21.      private AnyType [ ] theArray;
  22.     private int         topOfStack;
  23.  
  24.    
  25.  
  26.      
  27.      /**
  28.      * Construct the stack.
  29.      */
  30.     public ArrayStack( )
  31.     {
  32.         theArray = (AnyType []) new Object[ 10 ];
  33.         topOfStack = -1;
  34.     }
  35.  
  36.     /**
  37.      * Test if the stack is logically empty.
  38.      * @return true if empty, false otherwise.
  39.      */
  40.     public boolean isEmpty( )
  41.     {
  42.         return topOfStack == -1;
  43.     }
  44.  
  45.     /**
  46.      * Make the stack logically empty.
  47.      */
  48.     public void makeEmpty( )
  49.     {
  50.         topOfStack = -1;
  51.     }
  52.  
  53.     /**
  54.      * Get the most recently inserted item in the stack.
  55.      * Does not alter the stack.
  56.      * @return the most recently inserted item in the stack.
  57.      * @throws UnderflowException if the stack is empty.
  58.      */
  59.     public AnyType peek( )
  60.     {
  61.         if( isEmpty( ) )
  62.             throw new UnderflowException( "ArrayStack top" );   //  UnderflowException
  63.         return theArray[ topOfStack ];
  64.     }
  65.  
  66.     /**
  67.      * Return and remove the most recently inserted item
  68.      * from the stack.
  69.      * @return the most recently inserted item in the stack.
  70.      * @throws Underflow if the stack is empty.
  71.      */
  72.     public AnyType pop( )
  73.     {
  74.         if( isEmpty( ) )
  75.             throw new UnderflowException( "ArrayStack pop" );
  76.         return theArray[ topOfStack-- ];
  77.     }
  78.  
  79.     /**
  80.      * Insert a new item into the stack.
  81.      * @param x the item to insert.
  82.      */
  83.     public void push( AnyType x )
  84.     {
  85.         if( topOfStack + 1 == theArray.length )
  86.             doubleArray( );
  87.         theArray[ ++topOfStack ] = x;
  88.     }
  89.  
  90.     /**
  91.      * Internal method to extend theArray.
  92.      */
  93.     private void doubleArray( )
  94.     {
  95.         AnyType [ ] newArray;
  96.  
  97.         newArray = (AnyType []) new Object[ theArray.length * 2 ];
  98.         for( int i = 0; i < theArray.length; i++ )
  99.             newArray[ i ] = theArray[ i ];
  100.         theArray = newArray;
  101.     }
  102.      
  103.     /*
  104.      * som skapar returerar en kopia av stacken
  105.      *
  106.      * (obs, inklusive innehållet i stacken om det finns).
  107.      *
  108.      * */
  109.    
  110.    /* public synchronized Object More ...clone() {
  111.                  try {
  112.                      @SuppressWarnings("unchecked")
  113.                          Vector<E> v = (Vector<E>) super.clone();
  114.                      v.elementData = Arrays.copyOf(elementData, elementCount);
  115.                      v.modCount = 0;
  116.                      return v;
  117.                  } catch (CloneNotSupportedException e) {
  118.                      // this shouldn't happen, since we are Cloneable
  119.                      throw new InternalError(e);
  120.                  }
  121.              }*/
  122.    
  123.     public ArrayStack copy () {
  124.        
  125.         ArrayStack copiedStack = new ArrayStack();
  126.        
  127.         copiedStack.topOfStack = topOfStack;
  128.         //copiedStack.theArray = Arrays.copyOf(theArray, topOfStack);
  129.         copiedStack.theArray = theArray;
  130.        
  131.         return copiedStack;
  132.        
  133.     }
  134.    
  135.     public int size() {
  136.         // TODO Auto-generated method stub
  137.         return topOfStack;
  138.     }
  139.      
  140.    
  141.    
  142.     public static void main (String [] arg)
  143.     {
  144.        /*java.util.Stack s =new java.util.Stack();
  145.          
  146.         */
  147.         //Assert.assertEquals(s.pop(), 5);
  148.         ArrayStack s = new ArrayStack();
  149.         try{
  150.            
  151.             s.push("A");
  152.             s.push("B");
  153.             s.push("C");
  154.            
  155.             /*System.out.println(s.peek());
  156.            
  157.            
  158.             System.out.println(s.pop());
  159.             System.out.println(s.pop());
  160.             System.out.println(s.pop());*/
  161.  
  162.             //ArrayStack copystack = s.copy();
  163.             /*  ArrayStack copiedStack = new ArrayStack();
  164.              
  165.              
  166.               copiedStack.copy();*/
  167.            
  168.             ArrayStack /*<AnyType> */ cStack = s.copy();
  169.             int length = cStack.size();
  170.             for(int i = 0; i < length+1; i++)
  171.             {
  172.                 //copiedStack.pop(); //Won't cause a warning, no matter to which type you cast (String, Float...), but will throw ClassCastException at runtime if the type is wrong
  173.                 //Assert.assertEquals(s.pop(), copiedStack.pop());
  174.                  Object spop = s.pop();
  175.                  Object cpop = cStack.pop();
  176.                 if (!spop.equals(cpop)){
  177.                    
  178.                     System.out.println ("The test failed, you method dont work, go fix it");
  179.                     break;
  180.                 }
  181.                // else (i == copiedStack.topOfStack){
  182.                     System.out.println ("they copy of the stack is Done s[ "  + spop+ " ]" + " c[ " +  cpop +" ]");
  183.                     //
  184.                    
  185.             }
  186.            
  187.                    
  188.             }
  189.             catch( UnderflowException e)
  190.             {
  191.               System.out.println(e);
  192.               System.out.println("Stack empty");
  193.              
  194.             }
  195.        
  196.        
  197.     }
  198.  
  199.      
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement