• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stack Push() Method Not Accepting Argument Paremeters

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to make a couple classes of a calculator for my college project. I have to make a stack class, entry class and have two enums TYPE and SYMBOL inherited to entry class. I have only included relevant code but essentially, my push method doesnt like the argument push(Entry i) and i cant work out why. i have a junit5 test for it that i'll also include, and the error i get is "The method push(Entry) in the type CalcStack is not applicable for the arguments (int)". If anyone can help i'd be over the moon! I've also included the UML if that helps

Screenshot-2020-10-17-at-18.21.21.png
UML of my classes
UML of my classes
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try line 16
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 31 is a closing paren, not a brace.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid you have fallen into a common pitfall. Your pop() method doesn't delete the top element; that is a potential memory leak. Since you are using a List, you can probably write return myList.remove(...); and get rid of the memory leak.
I would augment the stack class with an isEmpty() method.
 
Daniel Chivelly
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 31 is a closing paren, not a brace.



I might have accidentally added that whilst copying in the code, I was more questioning why my push(Entry i) method doesnt accept integers when testing the method.
 
Daniel Chivelly
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Afraid you have fallen into a common pitfall. Your pop() method doesn't delete the top element; that is a potential memory leak. Since you are using a List, you can probably write return myList.remove(...); and get rid of the memory leak.
I would augment the stack class with an isEmpty() method.



Could you explain how so? I have written the following test which passed the pop method before I made changes to the push method.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your push method requires an Entry as argument, but you have a double.

And, when you pop, you do not adjust the size variable.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Your push method requires an Entry as argument, but you have a double.

And, when you pop, you do not adjust the size variable.



Yes, the method as you defined receives an object of type Entry, and then you are pushing a float instead of an object would this works?:
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Chivelly wrote:. . . Could you explain how so? . . .

Your pop() method allows the previous top element to remain in the List.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wss I mistaken? Were you using remove() all along? If so, sorry.
 
Daniel Chivelly
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eduardo Rod wrote:

Piet Souris wrote:Your push method requires an Entry as argument, but you have a double.

And, when you pop, you do not adjust the size variable.



Yes, the method as you defined receives an object of type Entry, and then you are pushing a float instead of an object would this works?:




Thank you so much how did I not get that? stress and fixations haha thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic