aspose file tools*
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: 24187
    
  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: 24187
    
  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: 40064
    
  28
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: 40064
    
  28
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Classes and Projects Chapter 2 of Head start JAVA pool puzzle