Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Head First Java Chapter 3 Pool Puzzle question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, first time poster here, i'm just having a bit of trouble understanding one small part of this puzzle:



So this puzzle compiles an array of triangles and calculates the area for each, but my question is, how does it know how to calculate the area when the formula is at the very end? Wouldn't it have to be at the beginning of the while loop for it to be taken into account? I've actually reached up to the end of Chapter 5 in the book, hoping that things would start to become more clear but i keep running in to similar confusion where code at the end of my program affects portions at the beginning. Any help would be greatly appreciated!
 
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you could do to make it clearer is add proper indentation. Then it would be obvious that setArea() is a method just like main() (maybe you already determined that but indentation would make it jump out at you). So, yes, setArea() appears after the place where it is called in main(). The order of methods inside a class makes no difference.
Staff note (Knute Snortum):

The code in the post above was edited so that it now has good indentation.

 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!    Thank you for posting the code in code tags.
Since this is your first post, I added proper indentation, see It looks more readable now. Rest is cleared by Carey Brown.
 
Stefan Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, I think that clears it up, thanks!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it matters where you put that method since the main just calls it.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am a total newbie in Java here also trying to understand pool puzzle 3 in HFJ.

Can somebody advise me on the below?
    1. What does x = 27; do here? - im referring to line 21
    2. What does it mean in line 22,  Triangle t5 = ta[2]; //does this mean to make a new triangle object and name it ta[2]??

thank you
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jimmy java wrote:1. What does x = 27; do here? - im referring to line 21


That's an assignment statement. It assigns 27 to the variable x. Read more about assignment statements and variables here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

2. What does it mean in line 22,  Triangle t5 = ta[2]; //does this mean to make a new triangle object and name it ta[2]??


Again, that's an assignment statement that declares a variable t5 to reference any object of type Triangle. The right hand side of the = operator is a reference to the element in array ta with an index of 2 (the third element). Read more about arrays here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and about classes and objects here: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch!
 
Jimmy lim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thanks Junilu, I am trying to write out each statement to understand what each line means. however I am having trouble figuring out how does y = 4 in this example? wouldn't y = 27? since y=x.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jimmy lim wrote:Thanks Junilu, I am trying to write out each statement to understand what each line means. however I am having trouble figuring out how does y = 4 in this example? wouldn't y = 27? since y=x.


No. Think of it like this:

Say there's you and a friend. You're y and your friend is x.

I'm going to give your friend $4.  That's like x = 4.

Then I'm going to give you however much your friend has. That's like y = x.

Now I'm going to give your friend $27.  That's like x = 27 (the $4 I gave him previously magically disappears).

How much do you have now? $27 or $4? You still have $4, right? So me giving your friend a new amount didn't affect what you have. The same principle applies with the assignment statements you're confused about.

 
Jimmy lim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh right thanks. I get it now. But what is the point of assigning x=27 at the end? wouldn't the output be the same without that line?
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of these things is to see if you understand enough of how things work to be able to predict what the results would be. Any resemblance to code that actually does something useful is purely coincidental.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic