need help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satan
    New Member
    • Oct 2006
    • 28

    #1

    need help.

    I need a help in my method equalStack in my class StackClass.

    Code:
    public class StackClass
    {
        private  int maxStackSize;    //variable to store the maximum
                                      //stack size
        private  int stackTop;        //variable to point to the top
                                      //of the stack
        private  DataElement[] list;  //array of reference variables
    
            //default constructor
            //Create an array of size 100 to implement the stack.
            //Postcondition: The variable list contains the base
            //               address of the array, stackTop = 0, and
            //               maxStackSize = 100.
        public StackClass()
        {
            maxStackSize = 100;
            stackTop = 0;         //set stackTop to 0
            list = new DataElement[maxStackSize];   //create the array
        }//end default constructor
    
            //constructor with a parameter
            //Create an array of size stackSize to implement the stack.
            //Postcondition: The variable list contains the base
            //               address of the array, stackTop = 0, and
            //               maxStackSize = stackSize.
        public StackClass(int stackSize)
        {
            if(stackSize <= 0)
            {
               System.err.println("The size of the array to implement "
                                + "the stack must be positive.");
               System.err.println("Creating an array of size 100.");
    
               maxStackSize = 100;
            }
            else
               maxStackSize = stackSize;   //set the stack size to
                                           //the value specified by
                                           //the parameter stackSize
            stackTop = 0;    //set stackTop to 0
            list = new DataElement[maxStackSize]; //create the array
        }//end constructor
    
            //Method to initialize the stack to an empty state.
            //Postcondition: stackTop = 0
        public void initializeStack()
        {
            for(int i = 0; i < stackTop; i++)
                list[i] = null;
            stackTop = 0;
        }//end initializeStack
    
    
            //Method to determine whether the stack is empty.
            //Postcondition: Returns true if the stack is empty;
            //               otherwise, returns false.
        public boolean isEmptyStack()
        {
            return (stackTop == 0);
        }//end isEmptyStack
    
            //Method to determine whether the stack is full.
            //Postcondition: Returns true if the stack is full;
            //               otherwise, returns false.
        public boolean isFullStack()
        {
            return (stackTop == maxStackSize);
        }//end isFullStack
    
            //Method to add newItem to the stack.
            //Precondition: The stack exists and is not full.
            //Postcondition: The stack is changed and newItem
            //               is added to the top of stack.
            //               If the stack is full, the method throws
            //               StackOverflowException
        public void push(DataElement newItem) throws StackOverflowException
        {
            if(isFullStack())
                throw new StackOverflowException();
    
            list[stackTop] = newItem.getCopy(); //add newItem at the
                                                //top of the stack
            stackTop++;                         //increment stackTop
        }//end push
    
    
            //Method to return the top element of the stack.
            //Precondition: The stack exists and is not empty.
            //Postcondition: If the stack is empty, the method throws
            //               StackUnderflowException; otherwise, a
            //               reference to a copy of the top element
            //               of the stack is returned.
        public DataElement top() throws StackUnderflowException
        {
            if(isEmptyStack())
                throw new StackUnderflowException();
    
            DataElement temp = list[stackTop - 1].getCopy();
    
            return temp;
        }//end top
    
            //Method to remove the top element of the stack.
            //Precondition: The stack exists and is not empty.
            //Postcondition: The stack is changed and the top
            //               element is removed from the stack.
            //               If the stack is empty, the method throws
            //               StackUnderflowException
        public void pop() throws StackUnderflowException
        {
            if(isEmptyStack())
               throw new StackUnderflowException();
    
            stackTop--;       //decrement stackTop
            list[stackTop] = null;
        }//end pop
    
    
            //Method to make a copy of otherStack.
            //This method is used only to implement the methods
            //copyStack and copy constructor
            //Postcondition: A copy of otherStack is created and
            //               assigned to this stack.
        private void copy(StackClass otherStack)
        {
             list = null;
             System.gc();
    
             maxStackSize = otherStack.maxStackSize;
             stackTop = otherStack.stackTop;
    
             list = new DataElement[maxStackSize];
    
                   //copy otherStack into this stack
             for(int i = 0; i < stackTop; i++)
                 list[i] = otherStack.list[i].getCopy();
        }//end copy
    
    
             //copy constructor
        public StackClass(StackClass otherStack)
        {
            copy(otherStack);
        }//end constructor
    
            //Method to make a copy of otherStack.
            //Postcondition: A copy of otherStack is created and
            //               assigned to this stack.
        public void copyStack(StackClass otherStack)
        {
            if(this != otherStack) //avoid self-copy
               copy(otherStack);
        }//end copyStack
    
    
            //Method to determine if a given stack has the
            //same content as the current one
            //Postcondition: Returns true if the two stacks have
            //               same content, false otherwise.
       public boolean equalStack(StackClass otherStack)
    	{
    		if (this.isEmptyStack() == otherStack.isEmptyStack())
    	 	{
    			try
    			{
    				while(top().compareTo( otherStack.top())==0)
    				{
    					pop();
    					otherStack.pop();
    				}
    					return false ;
    			}
    			catch (StackUnderflowException e)
    			{
    				return true;
    			}
    		}
    		else
    			return false;
    	}
    }

    Code:
    import java.util.*;
    
    public class TestProgStack
    {
    	public static void main(String[] args)
    	{
    		StackClass stack1 = new StackClass();
    		StackClass stack2 = new StackClass();
    
            try
            {
                stack1.push(new IntElement(5));
                stack1.push(new IntElement(7));
                stack1.push(new IntElement(1));
                stack1.push(new IntElement(3));
                stack1.push(new IntElement(2));
            }
            catch(StackOverflowException sofe)
            {
                System.out.println(sofe.toString());
                System.exit(0);
            }
    
            stack1.copyStack(stack1);
    
            System.out.print("stack1 elements are: ");
    
            while (!stack1.isEmptyStack())
            {
    			System.out.print(stack1.top() + " ");
    			stack1.pop();
    		  }
    	   	System.out.println();
    
            try
    		{
    		    stack2.push(new IntElement(1));
    		    stack2.push(new IntElement(3));
    		    stack2.push(new IntElement(2));
    		    stack2.push(new IntElement(5));
    		    stack2.push(new IntElement(7));
    		    stack2.push(new IntElement(0));
    
    		}
    		catch(StackOverflowException sofe)
    		{
    		     System.out.println(sofe.toString());
    		     System.exit(0);
    		}
    
    		stack2.copyStack(stack2);
    
    		System.out.print("stack2 elements are: ");
    
    		while (!stack2.isEmptyStack())
    		{
    		     System.out.print(stack2.top() + " ");
    			  stack2.pop();
    	   }
    		System.out.println();
    
    		if(stack1.equalStack(stack2))
    		System.out.println("The two Stacks have the same content");
    		else
    		System.out.println("The two Stacks have NOT the same content");
    		
    	}
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Code:
    //How about making use of the size of the stack 
    public boolean equalStack(StackClass otherStack) {
    	if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
    		return false;
    	}
    	else {
    		while(!isEmpty()) {
    			if(top().compareTo(otherStack.top()) != 0) {
    				return false;
    			}
    			else {
    				try {
    					pop();
    					otherStack.pop();
    				}
    				catch (StackUnderflowException e) {//This will no longer be neccessary?
    					return true;
    				}
    
    			}
    		}
    	}
    	return true;
    }

    Comment

    • satan
      New Member
      • Oct 2006
      • 28

      #3
      [QUOTE=r035198x]
      Code:
      //How about making use of the size of the stack 
      public boolean equalStack(StackClass otherStack) {
      	if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
      		return false;
      	}
      	else {
      		while(!isEmpty()) {
      			if(top().compareTo(otherStack.top()) != 0) {
      				return false;
      			}
      			else {
      				try {
      					pop();
      					otherStack.pop();
      				}
      				catch (StackUnderflowException e) {//This will no longer be neccessary?
      					return true;
      				}
      
      			}
      		}
      	}
      	return true;
      }
      [/QUOTE


      I've tried iy but it's not working.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        [QUOTE=satan]
        Originally posted by r035198x
        Code:
        //How about making use of the size of the stack 
        public boolean equalStack(StackClass otherStack) {
        	if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
        		return false;
        	}
        	else {
        		while(!isEmpty()) {
        			if(top().compareTo(otherStack.top()) != 0) {
        				return false;
        			}
        			else {
        				try {
        					pop();
        					otherStack.pop();
        				}
        				catch (StackUnderflowException e) {//This will no longer be neccessary?
        					return true;
        				}
        
        			}
        		}
        	}
        	return true;
        }
        [/QUOTE


        I've tried iy but it's not working.
        Where is it not working? Is it not compiling? Please explain the problem more clearly.

        Comment

        • satan
          New Member
          • Oct 2006
          • 28

          #5
          [QUOTE=r035198x]
          Originally posted by satan

          Where is it not working? Is it not compiling? Please explain the problem more clearly.
          Yes, it's not compiling. it can not find the symbol getSize in my method StackClass

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            [QUOTE=satan]
            Originally posted by r035198x
            Yes, it's not compiling. it can not find the symbol getSize in my method StackClass
            Ah now surely you should have realised that you need that method for your stack and put it in. Write a method that returns the number of items in the stack and call it getSize, if you already have one use it in place of getSize.

            Comment

            Working...