• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HeadFirst Java Chapter 9: 5 minute Mystery page 268

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great explanation. I was also confused in how they determined the power numbers since it seemed like the code was never called. I double checked the book and it isn't there. However, when I look at the code included in this thread there is additional code in main.

Does that mean the book was wrong? How would I know that? It didn't make sense to me until I found this answer here and saw the "added" code. I just don't want to waste time on future examples if they are incorrect.

The lines that seemed to be added (and are in this answer thread but not the book) are:

 
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic