• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Java Chapter 9: 5 minute Mystery page 268  RSS feed

 
Jonathan Grossman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

Very new to Java, and my first post within coderanch! i am working slowly through the HeadFirst Java book and a tutorial package from Udemy.

With regards to the 5 minute mystery on chapter 9, page 268 of HeadFirst Java, the answer explains that the total power use would have been 120, not 100 that Sarah's expected Ratios predicted. I understand the the V3Radiator invokes the V2Radiator constructor because it extends the class, but I do not understand how the result translates into "total power of 120". Please would you be able to assist?

Many thanks!

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably get more help if you provided more context around what exactly is being asked. Not everyone has that book and not everyone who has that book will be so inclined to dig through it to find the material you are referencing. On the other hand, whoever does take the time to do the digging that you're implicitly asking folks to do is going to be a rare but highly motivated helper. Good luck.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am rare and highly motivated!  Junilu sez so!

So what in this code don't you understand?

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Knute. 

@Jonathan, how do you figure the math to come up with 100 for the result?

I see the following added to the list:

@5 V2Radiator from line 10 + 30
@5 V2Radiator from line 11 + line 38 + line 30
@10 V3 Radiator from line 11 + line 39
@20 RetentionBot from line 12

RetentionBot has powerUse of 2 => 2 * 20 = 40
Anything else has powerUse of 4 => 4 * 20 = 80

Total power = 40 + 80 = 120
 
Jonathan Grossman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, many thanks for your responses!

Junilu, that truly has helped with my understanding! I did not initially grasp how the return values of 2 and 4 were used.

If it isn't too much trouble, would you please be able to help with two additional queries:

1. How/where the method powerUse is called in order for the "if statement" to run returning 2 and 4 for each type of SimUnit object?

2. Within the answers of the book it states: "Writing a constructor for the SimUnit class, that printed out a line everytime a SimUnit was created, would have quickly highlighted the problem." I have developed the code below which counts each SimUnit object as it is included within each array of V2Radiator, V3Radiator and RetentionBot. Is there a way of showing how many objects are then contained within each array?



Many thanks in advance!

 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Grossman wrote:
1. How/where the method powerUse is called in order for the "if statement" to run returning 2 and 4 for each type of SimUnit object?


It's called in main():


Do you know the extended for loop syntax?  Every element in aList is iterated through one by one and set as the variable o.  But o is of type Object, so you have to cast it to SimUnit.  Now, once cast, you can call powerUse().

2. Within the answers of the book it states: "Writing a constructor for the SimUnit class, that printed out a line everytime a SimUnit was created, would have quickly highlighted the problem." I have developed the code below which counts each SimUnit object as it is included within each array of V2Radiator, V3Radiator and RetentionBot. Is there a way of showing how many objects are then contained within each array?


Not that I know of.  None of the arrays are in scope at that time.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there is only one array in the program: the standard String[] args passed to main(). The program instantiates a single ArrayList object and passes around a reference to it throughout the program. You can use the list.size() method at any point to see how many elements it currently has.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!