my dog learned polymorphism*
The moose likes Beginning Java and the fly likes i dont ubderstand why i get a bug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i dont ubderstand why i get a bug" Watch "i dont ubderstand why i get a bug" New topic
Author

i dont ubderstand why i get a bug

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
when i wrote a function that pops a member from a stack
my logic saya that first you get that member
then decrease the place index by 1
and this way i get a bug that says "code is not reachable"

but my book tells to do it other wise
first decrease then pop the member
i cant ubderstand this
because this way we get the previos member
and not the last member

this is how i do:
==============================
public Object pop()
{
if (!isEmpty())
{
return data[sp];
sp = sp - 1; //the problem is here

}
Else
{
throw new EmptyStackException("Trying to pop from an empty Stack");
}
}

===========================================
and here is the answer of the book

public Object pop()
{
if (!isEmpty())
{
sp = sp - 1;
return data[sp];
}
Else
{
throw new EmptyStackException("Trying to pop from an empty Stack");
}
}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Kindly make use of the Code Tags whenever you paste your java code so that it looks neatly.

Conceptually, "popping an element from a stack" involves

  • Getting an element from the TOS (Top of the Stack)
  • Adjust the TOS accordingly (here, you decrease it by one).



  • If you do it the otherway, you will NOT get the *actual element* in the TOS rather the one which is next to TOS.

    The problem why you get code is not reachable is, you have a return statement prior to decrementing sp by 1. Once the control reaches a "return" statement, all the other following statements will NOT get executed.

    Put the return statement as the last statement in the method! It should work


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    johny doe
    Ranch Hand

    Joined: Dec 07, 2007
    Posts: 78
    so if i right the pop code
    like that:

    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Exactly.
    Bill Shirley
    Ranch Hand

    Joined: Nov 08, 2007
    Posts: 457
    I would guess that the code from the example is using the convention that the index as the next open location in the array to place something into the stack. Thus:



    Your (corrected) code would be the one in use if the index was pointing at the top most element in the stack.

    The former convention is often used because it simplifies (the appearance of) some of the code.


    Bill Shirley - bshirley - frazerbilt.com
    if (Posts < 30) you.read( JavaRanchFAQ);
    Rahim Vindhani
    Greenhorn

    Joined: Dec 06, 2005
    Posts: 10
    Even shorter use,



    Rahim Vindhani,<br /><a href="http://www.java-examples.com" target="_blank" rel="nofollow">Java Examples</a>
     
    GeeCON Prague 2014
     
    subject: i dont ubderstand why i get a bug