• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Print Object Name

Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Posts: 64714
  • 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,
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!