• 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

Head first Java - Chapter 4 Pool Puzzle p.91

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This puzzle is tripping me up for some reason. For instance, result = result + obs[x].doStuff(x); Why the need for doStuff(x)? Also, it doesn't give "factor" a value and it seems it should have one? Here is the code and the output for the puzzle:

public class Puzzle4 {
public static void main(String[] args) {
Puzzle4b[] obs = new Puzzle4b[6];
int y = 1;
int x = 0;
int result = 0;
while (x < 6) {
obs[x] = new Puzzle4b();
obs[x].ivar = y;
y = y * 10;
x = x + 1;
}
x = 6;
while ( x > 0 ) {
x = x - 1;
result = result + obs[x].doStuff(x);
}
System.out.println("result " + result);
}
}
class Puzzle4b {
int ivar;
public int doStuff(int factor) {
if (ivar > 100) {
return ivar * factor;
}else{
return ivar * ( 5 - factor );
}
}
}

Output:
%java Puzzle4
result 543345
 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all you should really use the code tags. You can use them by typing them:

[code]
Your code here
[/code]

Or by highlighting your code and then pressing the code button at the top of the message body page.

Second of all obs[x] is an index position in an array of type Puzzle4b. The class Puzzle4b has a method called doStuff(int factor) that takes an int that is called factor.

So when the program calls obs[x].doStuff(x) what it is doing is taking the object located in the Array obs at index x and executing that objects method doStuff which takes an int argument. The int argument that you are choosing to send to it is x.

So now the variable factor is equal to x when the program enters the method doStuff.
 
Taylor Street
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply David. It was very helpful! I have attempted to add some additional code to help me follow along with what is happening as the code runs for learning purposes; however, I can't seem to get it to compile correctly after I added the additional code and System out. What am I doing wrong? Maybe trying to learn the Java Util Logging (JUL) would be better?

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will tell you what you are doing wrong:

1) Puzzle4 is not the same as Puzzle4b and so you cannot use one when the other is needed.
2) "system" is not the same as "System" since Java is cAsE sEnsiTive.

Also, it will help us understand your code better if it is indented properly. Isn't this easier to read?:


Much luck!
 
Taylor Street
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pete, thank you so much! It worked! With my additional System out, I am much closer to understanding. The first loop reads: When x was 5 the doStuff method returned a value of 500000. How can that be? "ivar" takes the value of "y" and the value of "y" is 10 and never changes.
 
Taylor Street
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I answered my own last question. I was missing the point that once the int or factor was assigned x, that is how many zeros it will have prior to being multiplied by y or 10. Thanks so much for all the help. This forum is great!
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm doing the same exercise. These 'Pool Puzzles' are pretty tough for me. I've had to cheat and look at the end of the chapter twice now. I understand now how it works but even entering the solutions code doesn't get me a working program. This is exactly what the solution is (pretty confident):

It compiles fine but when I go to run it, it throws

I see that your code was changed throughout your posts on this thread. Was this to fix this issue?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic