Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

issues with my stack when running the testing does not produce the desired output  RSS feed

 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when running the code below i get

STACK TESTING
4
8
8
9
The size of the stack is: 3200
The stack contains:
3
7
9

i should be getting
STACK TESTING

4

8

8

9

The size of the stack is: 3

The stack contains:

9

7

3
please any ideas here is my code

 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your implementation of the size() method is incorrect, isn't it? Shouldn't it return the number of entries on the stack? That's what the comments in the interface say, anyway, but that's not what you are doing. And hence you end up with a backing array which can hold 3200 entries.

I predict that if you pushed another entry onto the stack, then your backing array would be changed to hold 6400 entries.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what is wrong with the output?

For example I see the output is double spaced and I assume that you want it to be single spaced.  What else?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say the last part of the output should be:

Why do you think that?  The head (top) of the stack can be the first element or the last, depending on the implementation.  You made it the last and that's fine.
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so how do i show the size correctly?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonothon Turner wrote:okay so how do i show the size correctly?


You stack class has a variable that is based on the number of elements in the stack. After all, how are you figuring out where to place the items in the array?  Calculate the size based on that variable.

Henry
 
Jonothon Turner
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i finally got it to work
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great!  Would you like to post your code?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!