Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Need help understanding puzzle from Head First Java p91

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hoping someone can help me understand the following code from Head First Java. I managed to solve the puzzle through much trial and error however I still don't quite understand how it is coming to the result displayed in the solution. The console returns the text "Result: 543345" when I run the package but I'm getting completely lost following the maths. Really want to understand what is happening here before I move on in the book in case I've missed something, can someone enlighten me?

From my maths the "ivar" variable should have a value of 100000 by the time the first while loop exits but that doesn't seem to add up properly. Have pasted codes from both class files below if someone cares to take a look, did a bit of searching around online to see if I could find more info but they were all help solving the project rather than explaining the code itself. Cheers!



 
Marshal
Posts: 79530
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I haven’t got my page 91 (or any of the other pages) to hand at the moment, so I can’t confirm that you have copied the code correctly.
What they want you to do is go through the code with a pencil and paper. Find whoever said you would not need pencil and paper again, and shoot them. Then write down the initial values of x yIf you add columns for ivar*factor and ivar*(5-factor) you can add those values up and I hope get 543345. That is the idea behind those puzzles: to get you used to following that value of variables as execution proceeds.
 
Dan Laidlow
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers for that, I actually tried doing it on paper earlier but must have got my wires crossed. This time I did it and it all clicked into place in my mind, totally understand it now!

Thanks
 
Campbell Ritchie
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
What are your superhero powers? Go ahead and try them on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic