• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Stacks using Arrays

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, this is a homework assignment I've been working on. From the existing StackClass class I removed the use of DataElement and IntElement (as requested by our teacher). But in changing my methods to do so I messed up somewhere along the way. It compiles, tells me whether the stack is empty or not, but seems when I put values in and try to print the stack with toString I am losing that stack, it is then appearing empty. I'm not quite sure what I did wrong. But it is really delaying me in my assignment in that we were really suppose to be concentrating on our reversePrint method that we are suppose to add. I even took this to the school tutor, with no luck she could not find the problem either. She did not know why my stack was being lost along the way. But I believe I narrowed it down to happening in the toString Method. Someone please check my code....



 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I don't know if I will be helpful or not, but I did want to point out a few things... the first thing, take a close look at your StackClass constructor, the one that takes an integer. maxSize is the name used for the value you pass in AND for the private variable that holds the max size for the class. so inside that method, whenever you say "maxSize", you are only referring to the value you passed in, and therefore you are never updating the private variable, so it will remain at 0. I think that is the main source of your problem, however that wasn't the only thing I noticed. You are pushing 8 things onto the stack, so make sure you have the space on the stack to push them all (ie. do not make your stack size 5 and then try to push 8 elements onto it). And two more things:
(1) in your push() method, you are comparing the length of the array to maxSize, but they are always going to be equal. test StackArray.length against maxSize inside any class instance, you will see that they are equal. "top" might come in handy here...
(2) take a closer look at this line:

maybe you're not starting at the very beginning of the stack...
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I did get it working. I changed quite a few things. I'm now going to start a new thread because my next question is on the reverse method.
Thanks for your help!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic