System.out.println() is your best friend when trying to figure out stuff like this. sprinkle them all throughout your code, and print 'interesting' things. I might add one on line 19 to print what e1.count has become after the +1.
Then, I'd put another one before line 21, printing what both e1.count and e2.count are, and ANOTHER one after it, again printing both variables.
You can always comment them out again as things become clearer. I don't delete them entirely, since I often come back and want to see parts again. It's much easier to un-comment them than re-create them.
Eventually, you can put in logging calls, like log4j, which makes it even easier, but that's a little advanced for where you seem to be (no offense).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I am a newbie here, and have tried several exercises from Head's First Java, can someone help me with the logic of this:
Where the result is:
And I have tried to change the last code to become:
Which resulted the same.
Have examined this for few days but to no avail.
Any help on the logic to get the answer "46" in both cases above mentioned would be much appreciated.
[Edit - added code tags - MB]
hello Ian Paul Budiman,
about your Q that you have posted, there is one thing is that , whenever any object is pointing to any reference variable just like Echo e1 = e2; hence whatever you do operation on either of variable it will be equally reflected to other reference variable or object...
fred rosenberger wrote:System.out.println() is your best friend when trying to figure out stuff like this...
@Ian: and just to add to what Fred said, so is toString(). I generally make it one of the first methods I write for all my classes, eg:and then for testing, wherever you think you need it, you can call:
System.out.println(e1); which will result in something like:
I'll leave you to read up about String.format() (a very useful method).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here