Exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • c0426
    New Member
    • Nov 2008
    • 5

    Exceptions

    Hello all! I'm relatively new to Java, so I'm sorry if this question comes off as naive. I am creating an array-based stack class. The goal of it is to work on exceptions.

    I have 2 classes, a test class and a regular class. The regular class has the methods object peek(), object push(), and object pop(). For the peek and the pop methods I am using an EmptyStackExcep tion. For some reason, the EmptyStackExcep tion will not allow me to enter in parameters for it like my exception for the push method does. Therefore, when I put it in the test class and my stack is empty, it doesn't check the exception. This part of my class looks like this:
    [code=java]
    public Object peek() // Returns object on top of this stack without removing it from the stack. Throws an EmptyStackExcep tion if this stack is empty
    {
    if( top == -1 )
    {
    throw new EmptyStackExcep tion();
    }
    return stk[top];
    }

    public Object pop() // Removes and returns object at top of this stack. Throws an EmptyStackExcep tion if the stack is empty
    {
    if( top == -1 )
    {
    throw new EmptyStackExcep tion();
    }
    return stk[top--];

    }
    [/code]
    And in the test class I put..
    [code=java] try
    {
    System.out.prin tln(newStack.pu sh("tart"));
    System.out.prin tln(newStack.pu sh("chocolate") );
    System.out.prin tln(newStack.po p());
    System.out.prin tln(newStack.po p());
    System.out.prin tln(newStack.po p());
    }
    catch(EmptyStac kException emptyEmpty)
    {
    System.out.prin tln("Stack is empty - could not carry out operation");
    }
    [/code]
    When complied, the output reads
    tart
    chocolate
    chocolate
    tart
    null


    Could somebody please tell me if they see something wrong in my coding, and perhaps lead me to a solution? Thank you so much!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Most likely the top variable is not being updated properyly. What does your pop method look like?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      What happens when you do the following in your test class:

      Code:
      while (true)
         System.out.println(newStack.pop());
      kind regards,

      Jos

      Comment

      • c0426
        New Member
        • Nov 2008
        • 5

        #4
        r, my pop method is as follows.
        public Object pop() // Removes and returns object at top of this stack. Throws an EmptyStackExcep tion if the stack is empty
        {
        if( top == -1 )
        {
        throw new EmptyStackExcep tion();
        }
        return stk[top--];

        }

        Jos,
        are you implying that I should put the while loop in the try/catch block?

        Thank you both!

        Comment

        • c0426
          New Member
          • Nov 2008
          • 5

          #5
          Oh Jos, I understand what you are saying. The goal of the assignment though is to utilize a try/catch block for an exception that is thrown. So I am supposed to handle the EmptyStackExcep tion using the try/catch block. I just don't see how this is possible if the complier will not allow me to pass a message in the exception's parameters.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by c0426
            Oh Jos, I understand what you are saying. The goal of the assignment though is to utilize a try/catch block for an exception that is thrown. So I am supposed to handle the EmptyStackExcep tion using the try/catch block. I just don't see how this is possible if the complier will not allow me to pass a message in the exception's parameters.
            That's because the EmptyStackExcep tion is (also?) an existing Exception.
            Can you show us your entire Stack class? I suspect that the 'top' member
            variable equals zero initially.

            kind regards,

            Jos

            Comment

            • c0426
              New Member
              • Nov 2008
              • 5

              #7
              [CODE=java]import java.util.*;
              public class Stack
              {
              private Object stk[];
              private int top;


              public Stack()
              {
              stk= new Stack[50]; //creates a default setting of 50 objects in the stack.
              top= -1; //sets top of stack to -1 to show there is no objects in the stack yet.

              }

              public Stack(int size)
              {
              if(size < 0) //if the size passed is less than zero, the size is set to fifty.
              {
              size=50;
              }

              stk= new Object[size]; //otherwise, the size is set to whatever is passed in the parameters.
              }

              public boolean empty() //tests whether or not the stack is empty.
              {
              if(true)
              {
              return top==-1;
              }
              return false;
              }

              public Object peek() //looks at the top item without discarding it.
              {
              if( top == -1 ) //if the stack is empty, throws EmptyStackExcep tion.
              {
              throw new EmptyStackExcep tion();
              }
              return stk[top];
              }

              public Object pop() //removes and returns item on top of stack.
              {
              if( top == -1 )
              {
              throw new EmptyStackExcep tion();
              }
              return stk[top--];

              }

              public Object push(Object item) // pushes item on top of this stack and returns item.
              {
              if (top == stk.length -1) //if the stack is full, Throws IllegalStateExc eption.
              {
              throw new IllegalStateExc eption("The stack is too full!");
              }

              stk[++top]= item;
              return item;
              }

              public int getTop() //getter method for the top item on the stack.
              {
              return top;
              }
              }[/CODE]
              Last edited by r035198x; Nov 5 '08, 07:28 AM. Reason: added code tags. Please don't forget them next time.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by c0426
                Code:
                import java.util.*;
                public class Stack
                {
                    private Object stk[];
                    private int top;
                   
                   
                    public Stack()
                    {
                        stk= new Stack[50]; //creates a default setting of 50 objects in the stack.
                        top= -1; //sets top of stack to -1 to show there is no objects in the stack yet.
                       
                    }
                   
                    public Stack(int size)
                    {
                        if(size < 0) //if the size passed is less than zero, the size is set to fifty.
                        {
                          size=50; 
                        }
                       
                        stk= new Object[size]; //otherwise, the size is set to whatever is passed in the parameters.
                    }
                Both of these constructors are incorrect for different reasons:

                1) it should be 'stk= new Object[50]' in your first constructor
                2) in your second constructor you forgot to set top= -1.

                Your test code most likely used the second constructor.

                kind regards,

                Jos

                Comment

                • c0426
                  New Member
                  • Nov 2008
                  • 5

                  #9
                  ah perfect! Thank you so much!

                  Comment

                  Working...