Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Upp1;
- import java.util.*;
- // ArrayStack class
- // The Stack class represents a last-in-first-out (LIFO) stack of objects.
- // CONSTRUCTION: with no initializer
- //
- // ******************PUBLIC OPERATIONS*********************
- // void push( x ) --> Insert --> Remove most recently inserted item
- // AnyType peek( ) --> Return most recently inserted item
- // AnyType topAndPop( ) --> Return and remove most recent item
- // boolean isEmpty( ) --> Return true if empty; else false
- // void makeEmpty( ) --> Remove all items
- // ******************ERRORS********************************
- // top, pop, or topAndPop on empty stack
- public class ArrayStack<AnyType>
- {
- private AnyType [ ] theArray;
- private int topOfStack;
- /**
- * Construct the stack.
- */
- public ArrayStack( )
- {
- theArray = (AnyType []) new Object[ 10 ];
- topOfStack = -1;
- }
- /**
- * Test if the stack is logically empty.
- * @return true if empty, false otherwise.
- */
- public boolean isEmpty( )
- {
- return topOfStack == -1;
- }
- /**
- * Make the stack logically empty.
- */
- public void makeEmpty( )
- {
- topOfStack = -1;
- }
- /**
- * Get the most recently inserted item in the stack.
- * Does not alter the stack.
- * @return the most recently inserted item in the stack.
- * @throws UnderflowException if the stack is empty.
- */
- public AnyType peek( )
- {
- if( isEmpty( ) )
- throw new UnderflowException( "ArrayStack top" ); // UnderflowException
- return theArray[ topOfStack ];
- }
- /**
- * Return and remove the most recently inserted item
- * from the stack.
- * @return the most recently inserted item in the stack.
- * @throws Underflow if the stack is empty.
- */
- public AnyType pop( )
- {
- if( isEmpty( ) )
- throw new UnderflowException( "ArrayStack pop" );
- return theArray[ topOfStack-- ];
- }
- /**
- * Insert a new item into the stack.
- * @param x the item to insert.
- */
- public void push( AnyType x )
- {
- if( topOfStack + 1 == theArray.length )
- doubleArray( );
- theArray[ ++topOfStack ] = x;
- }
- /**
- * Internal method to extend theArray.
- */
- private void doubleArray( )
- {
- AnyType [ ] newArray;
- newArray = (AnyType []) new Object[ theArray.length * 2 ];
- for( int i = 0; i < theArray.length; i++ )
- newArray[ i ] = theArray[ i ];
- theArray = newArray;
- }
- /*
- * som skapar returerar en kopia av stacken
- *
- * (obs, inklusive innehållet i stacken om det finns).
- *
- * */
- /* public synchronized Object More ...clone() {
- try {
- @SuppressWarnings("unchecked")
- Vector<E> v = (Vector<E>) super.clone();
- v.elementData = Arrays.copyOf(elementData, elementCount);
- v.modCount = 0;
- return v;
- } catch (CloneNotSupportedException e) {
- // this shouldn't happen, since we are Cloneable
- throw new InternalError(e);
- }
- }*/
- public ArrayStack copy () {
- ArrayStack copiedStack = new ArrayStack();
- copiedStack.topOfStack = topOfStack;
- //copiedStack.theArray = Arrays.copyOf(theArray, topOfStack);
- copiedStack.theArray = theArray;
- return copiedStack;
- }
- public int size() {
- // TODO Auto-generated method stub
- return topOfStack;
- }
- public static void main (String [] arg)
- {
- /*java.util.Stack s =new java.util.Stack();
- */
- //Assert.assertEquals(s.pop(), 5);
- ArrayStack s = new ArrayStack();
- try{
- s.push("A");
- s.push("B");
- s.push("C");
- /*System.out.println(s.peek());
- System.out.println(s.pop());
- System.out.println(s.pop());
- System.out.println(s.pop());*/
- //ArrayStack copystack = s.copy();
- /* ArrayStack copiedStack = new ArrayStack();
- copiedStack.copy();*/
- ArrayStack /*<AnyType> */ cStack = s.copy();
- int length = cStack.size();
- for(int i = 0; i < length+1; i++)
- {
- //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
- //Assert.assertEquals(s.pop(), copiedStack.pop());
- Object spop = s.pop();
- Object cpop = cStack.pop();
- if (!spop.equals(cpop)){
- System.out.println ("The test failed, you method dont work, go fix it");
- break;
- }
- // else (i == copiedStack.topOfStack){
- System.out.println ("they copy of the stack is Done s[ " + spop+ " ]" + " c[ " + cpop +" ]");
- //
- }
- }
- catch( UnderflowException e)
- {
- System.out.println(e);
- System.out.println("Stack empty");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement