File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stack ADT help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stack ADT help" Watch "Stack ADT help" New topic
Author

Stack ADT help

David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Hey all im not all that new to java but i am new to this Stack concept.

Here is my question.. I am asked to write a java program test.java that reads a sequence of 20 integers, pushing all of the even integers onto one stack and all the odd integers onto the other. Then i have to output the contents of each stack by popping elements until its empty.

I have a class that i have to use to do this. Its called ArrayStack.java

And the BoundedStackInterface.java is this:

The output needs to look like this when all said and done:

Now i am dumbfounded, to say the least, on how to go about adding those integer to a "push" in the test.java file since i have to write that from scratch.

Any help would be great!
David

[ October 07, 2008: Message edited by: David G. ]
[ October 07, 2008: Message edited by: David G. ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
"David G" please read the important administrative private message I have just sent you.

CR
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Anyone?

David
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

So where are you stuck? What have you tried? How did it fail?


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Patience, patience . . .

I would suggest later on you alter your Stack class to a parameterised type; you can find details in
the Java Tutorials.

Your pop() method won't work. What you have to do is retrieve the ith element, set its previous location to null and return it. So you need a return type for that method.

Have you been told to call the method which inspects the top element top()? It is usually called peek(). That method will work, but is awkwardly written; you can reduce it to a single line, or two statements if you are including the if-throw statement.

You are using the awkward if-else syntax for your isFull() and isEmpty() methods. That can be simplified greatly. See the coding conventions and look for �10.5.2.

When you have got it to work, it would be better parameterised, then you can change all the types to E and declare it as ArrayStack<Integer>.
To push 20 integers, then get back the even numbers only, is actually quite easy once you have done the bit of lateral thinking . . .



Try it with two stacks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Your push method will work, but the if (!something) . . . else reads awkwardly; see if you can get it into if (something) . . . else format.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
And why have you given protected access to the fields? They ought to be private.
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
This is what i am working with to test it..

However i get this error:

Yeah, not sure what i need to do because i know its not void because it pass a value before retrieving it.

David
[ October 09, 2008: Message edited by: David Goins.... ]
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Anybody see a problem with the code i just posted??

David
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Originally posted by myself:
Patience, patience . . .

Your pop() method won't work. What you have to do is retrieve the ith element, set its previous location to null and return it. So you need a return type for that method.


Please confirm you have read it.
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Originally posted by Campbell Ritchie:


Please confirm you have read it.


Yes i have read the comment but i am unable to change the structure of the other code as i have to use it as-is. The only thing i can change is the code i write myself, the PEX3 class.

David
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Why didn't you say so earlier? That sounds like a dreadful assignment. And there I was thinking you were doing something silly. You have been dropped in it and I am very sorry I thought that.


The easiest way round that is probably to print top() then call pop() to get rid of the top member.
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Ok thanks Campbell Ritchie but i still have the error on the pop() statment.

Here is the updated code:

The top() works just fine and displays "1" but once i add the pop it has the error.

David
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

"David Goins....", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
I wasn't clear; when I said call pop, I just meant myStack.pop(); Not print or anything. And make sure to put them in the correct order, print top, then pop.
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Ok thanks for clearing that up Campbell Ritchie.

It seems to work but i am having problems with the output now...

I'm trying to get a list of all the evens with "evens = even.top();" and then keep looping until its empty so i can have the output look like this "10 8 36 100 26 88 4 2 16" But right now it has an error saying:

If i just do this:

Then it outputs the numbers i posted above down the line like so:


David
David Goins
Greenhorn

Joined: Oct 07, 2008
Posts: 20
Ok i finally got it myself..

Thank you to those who helped

David
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Well done.

Actually the bit of lateral thinking I had was to push all the numbers onto stack1, inspect them all with top, then pop. If they are odd print, if even, push them onto stack2.
When stack1 is empty push the entire contents of stack2 back onto it, then print the entire contents of stack1.
Your splitting into odd stack and even stack appears neater and easier.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Nina Bussey,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack ADT help