aspose file tools*
The moose likes Beginning Java and the fly likes itemAt  Stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "itemAt  Stacks" Watch "itemAt  Stacks" New topic
Author

itemAt Stacks

Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I need to write a method to look at an itemAt a certain index, I believe that my precondition is right but I'm not sure how to search for the index. Would I need to create a temp stack in my method?
Thanks for any help.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You need to consider the structure of the java.util.Stack class (Bruce Eckel calls it a bit of bed design and I think I agree with him). It extends java.util.Vector, whihc is similar to java.util.ArrayList.

You can get all the functionality you require with an implementation of the java.util.List interface (at least I think so, if I have read your requirements right), and I would suggest java.util.ArrayList will give the best results.

And your precondition for throwing the Exception is miles out.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
Thanks for the suggestions. The comment about the precondition is confusing to me since my prof told me that I was using the correct approach as far as my precondition was concerned (that was before I added this part)

Object index = top;
index = 0;
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Are you trying to modify a Stack class that you already wrote? or are you trying to use the java.util.Stack class differently? I can't tell from your post.

Henry
[ April 06, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I'm trying to use the java.util.Stack to display what an item is at any given index with my itemAt() method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

As said, java.util.Stack extends java.util.Vector which has methods for retrieving an element from an arbitrary index. Check elementAt and preferrably get.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I'm supposed to use itemAt, isn't there and itemAt method in the Stack class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Originally posted by Teri Fisher:
I'm supposed to use itemAt, isn't there and itemAt method in the Stack class?
If your lecturer has told you to use a specific method then
you ought to have been told you which class to put that method in. It will probably have to go in the class you are writing.

As for whether there is an itemAt method in the java.util.Stack class, go and have a look for it in the API. (if no lower left box click OVERVIEW then java.util than Stack<E>.

And have you got your precondition working yet?
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I am writing it in a Stack class. No my precondition is not working yet, anything I've tried in my main gives me my NoSuchElementException.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Go through your precondition with a fine-toothed comb (or a pencil) and write down exactly what it does, then read it out, which will come to something like, "if x is greater than or equal to 1000000 or if x is an even number, then throw the exception." Something like. Not exactly like that.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I guess I am confused with my precondition (if 0<=n), am I correct that 0 stands for my Object index that I have declared? The requirements all came out of my book so I am thrown by the 0 in there, is it the same as if they had called it x? Just making things more difficult than I have to!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Originally posted by Teri Fisher:
I guess I am confused with my precondition (if 0<=n), am I correct that 0 stands for my Object index that I have declared? The requirements all came out of my book so I am thrown by the 0 in there, is it the same as if they had called it x? Just making things more difficult than I have to!!
It's not actually a precondition; a precondition is an instruction to a user which values to enter. By adding the "if" and the exception you have converted it to a guard, which like a precondition only much stronger. If your precondition is violated you are entitled to do anything, including returning an incorrect answer, but the guard ensures nothing happens when you do it.

Never mind that, which is advanced stuff. What follows is what really affects you.

Surely the object index is shown by n in the example you quoted? Please read what you have written very carefully and very slowly.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I've shifted things around but I haven't been able to pop the stack while (i<n)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Thank you for the e-mail.

You still haven't sorted out the precondition, so I presume you will still get an exception whenever you run your app.
As I said, read the if block where you are throwing the exception very slowly; you will kick yourself when you find out how simple it is to correct.

If you get into your while (i < n) loop you will never get out of it becase you are changing neither i nor n in the loop. You will need to reduce n while you are popping, or increase i.

I've shifted things around . . .
Don't. If you can't get soemthing to work, don't try shifting things. That usually makes it worse. Read the whole thing out very slowly, or write down what it is supposed to do with pencil and paper.

Good luck with it
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
My confusion with my precondition is that I was reading them wrong, I didn't realize that it was actually the range and I was supposed to write the precondition from that. Now that I am past that issue I am still trying to get my method to work properly. I have worked it out on paper and have come to this so far.

Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I think I just got it working! Maybe not perfect but I do have the expected output.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Well done.

I see you have got the precondition (or more precisely the guard) to work. I was going to ask for more code, but looking back I see you have posted it.

Shouldn't it be "return peek();" rather than "return index;"? You can miss out the two "this."s.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
Yes you are right, it should return peek(), I didn't need any index junk and I dumped the this.'s. Now I need to work on my errors when I give it an index that doesn't exist, because I get an EmptyStackException for three different places, I think it should just have display one error msg.
Thanks for your help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Originally posted by Teri Fisher:
I get an EmptyStackException.

Thanks for your help!
You're welcome. I know it would have been easier to give you the answer straight off, but you have worked it out for yourself and will remember a lot better like this.

It is reasonable to get an Exception if you are going outwith the bounds of the stack, but I take your point about amalgamating all the Exceptions into one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: itemAt Stacks