• Post Reply Bookmark Topic Watch Topic
  • New Topic

receiving a ClassName@4aa298b7 value instead of number  RSS feed

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently working on an assignment from programmingbydoing.com, called the NimGame. The exercise is to make three piles of three blocks. Each player can remove a chosen quantity from every pile. The person who makes the last pile zero, wins.
At first, I made the quantities of each pile with int variable declarations:


Then, I wanted to make some changes. I decided to make a new class, called NimPile. Then, I gave the first NimPile a value of three, but it seems that I can not really make calculations with it. When I compile and play the game, the variable a will receive a quantity of NimPile@somelettersandvalues

How do I solve this? How do I make the calculations work?
The new code is below. I only tested the NimPile-object A. B and C are in the original state.



Class NimPile looks super exciting by the way ;) :
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you see there is a default value for the result of NimPile's toString() method. Any time you convert an Object to a String to be shown to people, that Object's toString() is called to produce that String.

So if you modify the NimPile class to override the public String toString() method, you should start to see more useful information. Of course you would have to decide what information from NimPile you wanted to see, and then write code which produces a String from that information.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I am only slightly familiar with overriding.
What is the reason that I need to override the method toString() method? I want to understand this problem.

I made a class called NimPile, gave it one int variable called quantity.
Then, in Nim2 I created one object of the class NimPile and assigned the value 3 (an int value) to the int variable (quantity) of NimPile a. To me, this makes it able to make calculations to this variable of object. What goes wrong in my way of thinking?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Koorevaar wrote:What is the reason that I need to override the method toString() method? I want to understand this problem.

Because if you don't, it will inherit the method from Object which produces the output that you see, and apparently not what you want. You need to override the method to produce what you want.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding the toString method is easy:The hardest part is working out what to put instead of the ...
The String#format() method might be useful for that. You can get the name of the class if needed by getClass().getName() or getClass().getSimpleNam(). I suggest you don't put \n or %n or any other line end at the end of what you are returning. You can join Strings together, or to other types, with the + operator.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:Overriding the toString method is easy: . . .
And, the cheque's in the post.
And, somebody is on his way to help you already.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay guys. Based on your help and the following page:
http://www.javatpoint.com/understanding-toString%28%29-method
I presumed that simply


Would do the trick, but I receive the following error:
error: incompatible types: int cannot be converted to String
return quantity;
]

What is the missing link here?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'quantity' isn't a String, it's an int.

Maybe something like:

for the String?
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. It works for me now. Thanks everyone.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you may have gotten it to work, that doesn't mean it's good.

Your NimPile class is not only unexciting, it's quite pointless. Objects should encapsulate behavior and its associated data. The point of using objects and classes is to make your program more abstract and understandable. NimPile does none of these. In fact, I would say it just confuses the reader with its uselessness.

There are a few things you can do to make NimPile more useful though. One thing is to move those calculations that involve its quantity and state of emptiness from outside of NimPile to inside. So instead of asking a NimPile for its quantity and then determining if it's empty, you tell a NimPile to reduce its quantity by a certain amount. Then you tell the NimPile to report whether or not it's empty. Like so:


See how the code reads like a story rather than as a specification of multiple detailed calculations? That's because of the abstractions provided by encapsulating (and hiding) data and detailed calculations and control flow statements behind abstract names that reveal intent rather than implementation.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, there's an object oriented design principle related to what I said: Tell, Don't Ask Principle
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to leave the implementation of allPilesAreEmpty() as an exercise for you but I'll give you one possible implementation and I'll leave it as an exercise for you to make it better:

Again, notice how the method name provides an abstraction and a statement of the intent of the calculation that is made inside the method. In other words, you are revealing the intent of determining whether or not all piles are empty by hiding the calculation involved in that determination, thereby making the rest of your code clearer and better-organized.

This code is not too bad in that it achieves a level of abstraction and hiding but it could still be made better. For one thing, the calculation limits your program to having exactly three piles. What if you want to play with only one pile or maybe five piles? If you want to make your program be flexible about the number of piles to play with, then you'll have to change this logic. Failure to change this logic will result in a bug when you change the number of piles you're playing with. You can design the program to be more flexible and write this method in such a way that it will not require any changes regardless of how many piles your Nim game uses.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your insights. I just started java several months ago and I am willing to improve my programming skills. I will try to make the game work with your suggestions. I will come back to it later.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!