The moose likes Beginning Java and the fly likes Classes and Projects Chapter 2 of Head start JAVA pool puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classes and Projects Chapter 2 of Head start JAVA pool puzzle" Watch "Classes and Projects Chapter 2 of Head start JAVA pool puzzle" New topic
Author

Classes and Projects Chapter 2 of Head start JAVA pool puzzle

Peter Pay
Greenhorn

Joined: Sep 29, 2009
Posts: 7
I am trying to figure out when this code is compiled, the last line of the output is 10. This is a pool puzzle exercise from Chapter 2 of Head start JAVA book. The book gave the answer but I still don't understand why the output is 10 on the last line. Please help.

Code:

public class EchoTestDrive {

public static void main(String [] args) {
Echo e1 = new Echo();
Echo e2 = new Echo( );
- or -
Echo e2 = e1;

int x = 0;

while ( x < 4 ) {
e1.hello();
e1.count = e1.count + 1;

if ( x == 3 ) {
e2.count = e2.count + 1;
}

if ( x > 0 ) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class Echo {
int count = 0;
void hello( ) {
System.out.println(“helloooo... “);
}
}

OUTPUT:

helloooo...
helloooo...
helloooo...
helloooo...
10
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
what the hell.. it is clear cut. use pencil and paper and see yourself. on each iteration start assigning the right values. You will see that it should be 10. Tell us on which line you are stuck?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
See I am assuming you have written e2 = e1.
what do you mean when you write or there. Both have different meanings. In later case, bothe reference variables are pointing to same object and operating on same variable copy, while in first case, there are two copies of variable count in each object referenced by e1 and e2.
Peter Pay
Greenhorn

Joined: Sep 29, 2009
Posts: 7
The real problem that I am having is the line e1.count = e1.count + 1;

I can understand where the x value is run through the loop 4 times to get the "hellloooo" line but I am failing to see how the e2 adds up to be 10.

Every time the loop occurs, what is the value of e1.count?

Please understand that I have no programming background. And I believe this forum section is beginning java. If it is clear cut, I would not have ask for help.

Thanks for any basic help that I can get.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24189
    
  34

What Rahul has less-than-nicely suggested is actually the best plan for understanding stuff like this. Get a piece of paper -- graph paper works nicely -- and create a column for each variable. If you use e2 = new Echo(), you'll need columns for e1.count, e2.count, and x. If you use e2 = e1, then you have to realize that e1.count and e2.count refer to the same variable -- since e1 and e2 are two different names for the same object -- and so you need only columns for count and x. Then start at the top, and at each line, write down the values of each variable. When the code branches somewhere, you follow the branch. Pretend you're the computer, and execute each line of code, keeping track of those variables. At the end, you should come up with the same value the program does, and that's the best you can do in terms of "understanding." You see the steps the program takes, and you understand how it arrived at some value.

The line "e1.count = e1.count + 1" means to take the value of e1.count, add one to it, and store it back into e1.count; at the end, the value in e1.count is one larger than before.


[Jess in Action][AskingGoodQuestions]
Peter Pay
Greenhorn

Joined: Sep 29, 2009
Posts: 7
Maybe my problem is mixing the value of x with e1.count. I see the x value has to do with the loop.

So in loop run #1, is e1.count=0?

Loop run #2, e1.count=1?

Ok... maybe I may be asking a dumb question for a newbie but x is set to start on 0. I understand how that loop works.

I am not seeing the initial value of e1.count. Once I know the initial value, I can figure it out.

Thanks.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24189
    
  34

WHen an instance of the Echo class is created -- i.e., "Echo e1 = new Echo();" -- it contains its very own "count" variable. If you look at the definition for that class Echo, you'll see "int count = 0", so "count" starts at zero when e1 is created. All member variables are actually initialized to some 0-like value (0, 0.0, null, or false) by default, so the "= 0" there is redundant.
Peter Pay
Greenhorn

Joined: Sep 29, 2009
Posts: 7
Thanks. Now I understand.
tuğçe hilal
Greenhorn

Joined: Dec 05, 2009
Posts: 4
hi guys
i have the question
what does the assignment Echo e2=e1 mean in this code which is used instead ECHO E2=NEW ECHO()?
(it is for the output to be 24) Since i dont know what that(assigning a reference to the object?) really means i apologise now that i cant use paper and pencil
thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33
Welcome to JavaRanch

What do you think the assignment means? We like people to try to answer such things, then we have a look at the answer. Remember those examples in HFJ are contrived to make you think about what is happening.
tuğçe hilal
Greenhorn

Joined: Dec 05, 2009
Posts: 4
Actually i have learnt what it means and tried some things on paper which is far away from the answer. i just cant trace
tuğçe hilal
Greenhorn

Joined: Dec 05, 2009
Posts: 4
Echo e2 = new Echo( );
- or -
Echo e2 = e1;
when i trace i find the same thing with either this 2.although i know at first a new e2 object created while at the 2nd it just reference.when i try to trace i just simply give the same values in both ways
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33
No, the two are different. If you write new Echo() you get two objects. If you write e2 = e1 you get one object with two names (two identifiers pointing to the same reference).
Anshul Srivastava
Greenhorn

Joined: Jul 08, 2015
Posts: 4
Hello all! I have a problem into same question which asked above in Echo e2 = e1; while running the program on paper, here it is:

As suggested in post above try to fetch the value in two columns as count = 0 and x = 0 , I got:




I am confused at x = 1 when it's enters in if( x > 0 ) where e2.count = e2.count + e1.count; what is the value of e2.count ?
Well what I get when e1.count = 2, then it should be like e2.count = 2 + 2(e2=e1). e2 should have same value as e1 have?
Correct me if I am wrong .

P.S : I bought Head first java recently and starting digging into it and I am new to java.

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33
Welcome to the Ranch

I put code tags round your output and added a few spaces. Always use the tags; they work for such printouts as well as code, and doesn't it look better now
Anshul Srivastava
Greenhorn

Joined: Jul 08, 2015
Posts: 4
Okay,Sure, I will use it next time when I post it and Sorry for that mess but can you look at my problem which I mentioned above, it will be great.

And yeah it's looks great.

Thank You.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33
I presume this is the puzzle on page 44 of HFJ 2/e.

I presume you have worked out the Echo class in the smaller box.
You want hellooo... 4×, so you will want x < 4 after the while.
If you write Echo e2 = e1 in line 4, then e1.count and e2.count will be the same thing. Adding the two together is equivalent to doubling that count; it is only in one object so you see the same value in two different references. But what you see is the same value.
You cannot add 1 to count and then double it and get 10. So you need two different objects. So line 4 must be
Echo e2 = new Echo();
Then you need to add enough 1s to e1.count and to e2.count that when you add the two together you get 10.

Let's get the squared paper:-
Anshul Srivastava
Greenhorn

Joined: Jul 08, 2015
Posts: 4
Thanks for the brief explanation Sir, but can you explain what you mean by "You cannot add 1 to count and then double it and get 10."?
When I want the output 24 by Echo e2 = e1.
Can you explain this by iteration?

What I was doing is when the value of x = 1 it will count e1 = 2 and then it goes to double the value of e2.count = 4 and then continue by x = 2 then it count e1 = 3 and then doubles the value of count e2 = 6. And now x = 3 then again it count e1 = 4 and then goes to condition if(x == 3) where it count e2 = 5 (e1 = 4 in previous step) and then it add e1+ e2 = 9.
Something is going wrong after the if(x==3) condition and it confused with value of e2 into this. Please help me I'm totally confused.

Thanks for bearing with me!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33
Add 1 to 0 and you get 1. Double it and you get 2. Now see how you are going to keep adding 1 and doubling and get 10. I think you can get all sorts of things but I don't think you can get
((0 + 1) × 2 × 2 + 1) × 2)
out of that loop. I may be mistaken, though.
Anshul Srivastava
Greenhorn

Joined: Jul 08, 2015
Posts: 4
Thanks a lot! Now I got it, well I hope so.


Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43995
    
  33

And you're welcome.
 
Have you checked out Aspose?
 
subject: Classes and Projects Chapter 2 of Head start JAVA pool puzzle
 
It's not a secret anymore!