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
Ian Paul Budiman
Joined: Jan 05, 2012
It seems the answer is much more simpler than I thought.
I tried to put all the formula into an equation, and got stuck with "e1.count = e1.count + 1"; and "e2.count = e2.count + e1.count;" which then I got lost where to put e1=e2 in the middle.
Thanks for shedding the light.
The answer is: e1.count = e1.count * 2 + 2
And thanks for the input Fred, it's great, it gives me clearer picture......and I'll save log4j later...as your suggestion.
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).
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here