• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print Object Name  RSS feed

 
jack worthing
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Can any one help me please
I want to print the objects' name before each stack by the pop() method withot using SOP statement in the main()
this is my code



thankyou
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand the question. But you have not implemented the push and pop methods the usual way.

The push method takes a method parameter and adds it as the top member of the stack; the stack size is not one larger. Push doesn't usually return anything; you could return "true" if it worked, otherwise "false" if you so wish.

The pop method doesn't usually take a parameter; it takes the top member off the stack, sets that previous location on the stack to null, reduces the size by 1 then returns what was taken off the stack.

It is also odd design to have a stack inside another class. The stack is independent of the Volume class; you should implement a VolumeStack class. Also implement the isEmpty() and peek() methods. The peek() method returns the top element, without removing it from the stack.

I know of no way to print the names of variables other than writing literal Strings. You can use a Formatter to format to System.out, but that is little different from System.out.print. Not sure I understand that part of the problem. You can have a print() method in the Volume class,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!