• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Triangle Puzzle Question  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm very new to programming. Can someone tell me if I have a correct understanding with how the Triangle array would work in the While loop in
this part here:



I hope my question makes sense. When the code goes into the While loop the first time. Would the values look as the following? Thank You very much.






Here is the full code.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code has "smart quotes", that is, they lean to the left and right. Java needs straight quotes. Turn your text editor to text-only mode or use Notepad++ in Windows or Vim in Linux/Mac.

you should also indent you code so that the logic structure can be seen.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Your code has "smart quotes", that is, they lean to the left and right. Java needs straight quotes. Turn your text editor to text-only mode or use Notepad++ in Windows or Vim in Linux/Mac.

You should also indent you code so that the logic structure can be seen.


Hi Knut Snortum,

I know what you are referring to. This is occurred because I copied it from a pdf file. For my question I assumed no one would actually need to run the code so I didn't fix it. It was wrong of me to assume that and I won't moving forward. Can you please instruct me on how to edit a post? I don't see an option and searching for the answer didn't bring anything up. Thank You .
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually. Here is a copy of the full code without the "smart quotes".

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have 4 spaces in the array, and you go through it, putting a Triangle in each of the 4, then setting some values in that Triangle.
You then seem to take one of them and alter its area (without altering height and length ‍).

Then you have two names for the same triangle: ta[2] and t5. You can see as you go through the code that any changes applied to ta[2] appear in t5. That should hardly be surprising because ta[2] and t5 both point to the same object.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am sorry. What I'm trying to gain an understanding of is this snippet of code.



I already know what the output will be but what I am concerned about is whether I understand how the code in the while loop works. For the first loop my thinking the code is read as the following.



Thank You!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks correct, yes. Try writing down what the second triangle will look like.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That looks correct, yes. Try writing down what the second triangle will look like.


Thank You Very Much!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Kubiche Dominguez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well done


Hello everyone. Just joined the forum with this exercise from the book and I dont want to go further without understanding this. I understood the first part of the problem. What I dont understand
is from where comes the 343. Can anyone here explain to me please? I cant figure out when "y"  equals 4 neither.
What values were taken to multiply and divide so it ended in 343 or whatever was done.

Thanks in advance.
Regards


 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Kubiche Dominguez wrote:What I dont understand
is from where comes the 343. Can anyone here explain to me please? I cant figure out when "y"  equals 4 neither.
What values were taken to multiply and divide so it ended in 343 or whatever was done. 

343 is not the outcome of any calculation. They have just directly assigned 343 value to an instance variable area of object of Triangle which is at index 2 of array ta.
I think they did this, just to make us understand how you can make another reference variable to refer to an object by assigning It's reference like below
Here reference of an object at index 2 is assigned to reference variable t5, now t5 and ta[2] both refers to same object. They intentionally used ta[2] reference variable to assign value of instance varaible area     and used t5.area while printing It so we can understand both reference variables i.e. ta[2] and t5 referred to one same object, so you can access Or make changes to properties of that object using any of these reference variables.

y equals 4 because when x become 4 in while loop then x < 4 results false and control comes out of while loop. Then to the very next line they have assigned value of x to y here
so when printed y It prints 4.
 
Kubiche Dominguez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. Ok undestood the y equals 4, i totally forgot the while.
Now, why they assigned the value 27 to x again if its never used or theres
still something i need to understand. I thought the 27 would have an impact
on the new assigment of x because i dont see it. And yes, the thing on the chapter is
exactly what you saying refering different variables to one same object.
Thanks again.
Regards
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kubiche Dominguez wrote:Now, why they assigned the value 27 to x again if its never used or theres
still something i need to understand. I thought the 27 would have an impact
on the new assigment of x because i dont see it.
They want us to know, how assignment of reference variables differ from assignments of primitive data type variables, with their impact on the value they are having.
In the case of primitive data type variable assignments, herevalue of x is assigned Or we can say copied to variable y i.e. 4

Although we assign x = 27 It's not gonna affect the value of y so when we print the value of y then we get 4 and when we print value of x obviously we will get 27 
 
Kubiche Dominguez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!