This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes adding up variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding up variables" Watch "adding up variables" New topic
Author

adding up variables

David Vimsekh
Greenhorn

Joined: Feb 25, 2008
Posts: 5
Dear forum members:

I'm trying to figure out an exercise in a Java-How-to book. Can't figure out why the code below gives "e2.count" total as "7". Can someone show me by adding up the "e1's" and "e2's"? I know it revolves around the "Echo e2 = e1;" line of code, but I can't figure out how it adds to equate to "7." I've tried figuring it out by manipulating the "if (x...)" values to watch the count step-by-step, but when I get to the "x" values in the code below, I lose it.


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

The line

Echo e2 = e1;

means that e2 is just another name for e1, and in fact you can delete this line and change all "e2.count" to "e1.count" without changing the program in the least. Therefore the line

e2.count = e2.count + e1.count;

actually means "multiply e1.count by 2"! Does this make more sense now?


[Jess in Action][AskingGoodQuestions]
David Vimsekh
Greenhorn

Joined: Feb 25, 2008
Posts: 5
No it doesn't. How can e1 * 2 = 7, when numbers are all integers? Watch...

When x = 0

e1 = 1
e2 = 1 + 1 (e2 = 2)

when x = 1

e1 = 2
e2 = 2 + 2 (e2 = 4)

Ok. If I manipulate the variable x to be x < 2 in both cases, then the answer is 6... why? Why isn't it 4? This is why I asked for someone to just show the numbers associated with the "e's" so I can follow along. I would guess that the previous e2 is being added to the e2 in the above case to equate to 6, but that doesn't work in the case below.

when x = 2

e1 = 3
e2 = 3 + 3 (e2 = 6)

Ok. Here the statement e2 = e1 + e2 doesn't apply because it's ran only when x < 1.


Would someone just plug in the e1 and e2 values in each step so I can follow along?

Thank You
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
One key is that e1 and e2 point to the same object.

x=0

e1.count=e2.count = 0 //we will just call e1.count and e2.count, count from here on out

count = count + 1 //(0+1=1)
count = count + count //(1+1=2)
x=1
//end of first loop

count = count + 1//(2+1=3)
count = count + count//(3+3=6)
x=2
//end of second loop
count = count + 1//(6+1=7)
x == 2 so the if block does not execute
x=3
//end of third loop
//while block exits

walking through the code on paper is usually helpful. Adding in print statements to print out the value at certain points is also helpful.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
David Vimsekh
Greenhorn

Joined: Feb 25, 2008
Posts: 5
Yah, I did sit with a piece of paper and work out the steps, but something does not still make sense to me... (but thanks so far)...

It says: Echo e2 = e1

I read that as: "e2 becomes what e1 is"
In other words: e1 value --> e2

but not as: "e2 becomes what e1 is and e1 becomes what e2 is"
Same as: e1 value --> e2, e2 value --> e1

It's harder to discusss it with your example because you lumped together e1 and e2. But going back to the way it should look...

So when you say: e2.count + e1.count = 6 (which means e2 = 6)
then you say: e1.count + 1 = 7 (you just plugged in e2's value in for e1)

Which means what value is plugged in for e1 becomes e2, and vice-versa then?


How would one state that "Echo e2 = e1" but "e1 never becomes what value e2 holds" then?

meaning: e1 value --> e2, legal
but: e2 value --> e1, illegal

Is it possible to do that?

I should mention where I'm confused:

if "x = 0" in code, it doesn't mean that I can code "0 = x" and manipulate it as such in a Java program.
[ February 27, 2008: Message edited by: David Vimsekh ]
[ February 27, 2008: Message edited by: David Vimsekh ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Echo e1.count? You mean Echo e1; otherwise it would think e1.count is an Echo, rather than e1.

You start off with e1, which you have already been told has a count of 0.
Then you set up e2 and tell it to use the same object that e1 is already using. That is what e2 = e1; means. Nothing about vice versa; the assignment operator = works in one direction only, right-to-left, never left-to-right. As you said, you can say x = 0; but you can't say 0 = x;.

No, e1 never becomes what e2 used to be (e2 never had a value "of its own"). Try again; David McCombs has already explained how the loops work, and see if you can get 7 out of it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
You seem to be thinking that e1 and e2 are objects - they are not - they are object references.
creates an Echo object and sets e1 to reference this object.
sets e2 to reference the same object as e1. There is still only one Echo object which you can access using either e1 or e2.

Does that help ?


Joanne
David Vimsekh
Greenhorn

Joined: Feb 25, 2008
Posts: 5
Ok hold on here....

If it's not vice versa, then I'm totally lost. I have read what McCombs wrote...
But this is how I read it...



[ February 28, 2008: Message edited by: David Vimsekh ]
[ February 28, 2008: Message edited by: David Vimsekh ]
Lys Marie Santana dos Santos Silva
Greenhorn

Joined: Feb 26, 2008
Posts: 5
Originally posted by David McCombs:
One key is that e1 and e2 point to the same object.

x=0

e1.count=e2.count = 0 //we will just call e1.count and e2.count, count from here on out

count = count + 1 //(0+1=1)
count = count + count //(1+1=2)
x=1
//end of first loop

count = count + 1//(2+1=3)
count = count + count//(3+3=6)
x=2
//end of second loop
count = count + 1//(6+1=7)
x == 2 so the if block does not execute
x=3
//end of third loop
//while block exits

walking through the code on paper is usually helpful. Adding in print statements to print out the value at certain points is also helpful.


Thanks for the tip about add print statements to print out the values. Is really helpful and make the understanding more easy.


Lys Marie.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As I said way back in the first reply on this thread:


The line
Echo e2 = e1;
means that e2 is just another name for e1,


I guess if you don't understand about reference variables, this isn't specific enough. I also said that you should replace all "e2" with "e1" after this, since they're equivalent, and you said "that's too confusing". But in fact, that's the central point of this example. Try reading this and this. These two amusing short essays should hopefully clear this all up for you!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Originally posted by David Vimsekh:




e1 and e2 both point to the same object (see my previous post), therefore as soon as you set e2.count to a value, e1.count will have the same value (because it is the same variable).
David Vimsekh
Greenhorn

Joined: Feb 25, 2008
Posts: 5
I guess if you don't understand about reference variables, this isn't specific enough. I also said that you should replace all "e2" with "e1" after this, since they're equivalent, and you said "that's too confusing". But in fact, that's the central point of this example. Try reading this and this. These two amusing short essays should hopefully clear this all up for you!


Thank you. The second essay cleared it up. Head Start should realize the "beginner" programmer won't understand this off the top of their head. I know it's a bonus question, but how am I supposed to figure that out by myself when the book up to this point hadn't even alluded to how to understand this?

Thank You to everyone who helped me nail this one down. I can now move on to the next chapter. I'm looking forward to helping anyone in this forum when I can do so.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Originally posted by David Vimsekh:
Thank You to everyone who helped me . . .
You're welcome, and I am sure that applies to everybody. It is good to see we have been sorted things out successfully.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding up variables