• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classes and Projects Chapter 2 of Head start JAVA pool puzzle

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Peter Pay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Now I understand.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! Now I got it, well I hope so.


Thanks again.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And you're welcome.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also put conditions as follows in sequence.
x<4 (in while)
x>0 (in 1st if)
x>1 (in 2nd if)

the answer will be the same in both of the questions, you'll get 10 & 24 respectively. ;)
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the logic behind getting 10 as well as 24. Still, on the 8th line we did e1.hello() which means to pave the way for the variable e1 to access the hello() instance variables...my question is then why we are not using e2.hello() to get the desired output of helloooo... 4x times and then 10. I am a complete beginner in java.
 
Bikram Chakma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does e1.count means???
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic