This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

HeadFirst Java, Ch3 Pool Puzzle

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm a little stumped...the following code produces this output:

triangle 0, area = 4.0
triangle 1, area = 10.0
triangle 2, area = 18.0
triangle 3, area = 28.0
y = 4, t5 area = 343


What has me stumped is the last line of output...I understand that both t5 and ta[2] are pointing to the same Triangle object, and that by setting ta[2].area = 343, t5.area will also = 343. But where does "y = 4" come from - if y = x, and x is set as 27, where is the value changed to 4? Any help would be appreciated, thanks!
[ May 26, 2008: Message edited by: Campbell Ritchie ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is straight forward.

After the while loop you are setting y = x where the value of x is 4 at that time. Later you changed the value of x to 27 but not �Y� so it printed value of y as 4.
 
Yong Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thanks for the fast reply!

Second, duh - I hadn't thought about the value of x as the loop was exited...for some reason, I thought that since x was given a new value, y took the same value. I see now that since the line "y = x" was written before "x = 27", y retains the previous value of x. Thanks!
 
Marshal
Posts: 65772
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags round quoted code; it makes it easier to read. I shall edit your post to add tags.
 
Yong Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, will use code tags in the future. Thanks for the heads up.
 
Campbell Ritchie
Marshal
Posts: 65772
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Yong Lee:
Sure, will use code tags in the future. Thanks for the heads up.

Thank you. You see how much better it looks with the code tags added. There are buttons below the "message" box for formatting your posts.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an old thread, but i also had a question.

But i am also having a trouble understanding the last part.

On line 17 and 18, y was 4, then the value changed to 27. but it had no influence on printing out y as 4(line21).
On line 19 and 20, t5=ta[2]. i am thinking t5=18 from previous area ta[2]. Then why all of sudden from line 20, t5.area changed to 343? should it not have any influence on t5 value, and print out 18 as area(line22) instead of 343?

sorry to bump the old thread, but someone please answer my question.

Thanks.

 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first question is answered in this thread by Yong Lee.

As to your second question, ta[2] references a Triangle object. When you call

is a new object created, or does t5 reference the same object as ta[2]?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yong Lee wrote:So I'm a little stumped...the following code produces this output:

triangle 0, area = 4.0
triangle 1, area = 10.0
triangle 2, area = 18.0
triangle 3, area = 28.0
y = 4, t5 area = 343


What has me stumped is the last line of output...I understand that both t5 and ta[2] are pointing to the same Triangle object, and that by setting ta[2].area = 343, t5.area will also = 343. But where does "y = 4" come from - if y = x, and x is set as 27, where is the value changed to 4? Any help would be appreciated, thanks!
[ May 26, 2008: Message edited by: Campbell Ritchie ]


I realize this is an old post but I run the exact same code as above (chapter 3 HeadFirstJava),
and I receive the following :
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!