File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Head first Java - Chapter 4 Pool Puzzle p.91 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head first Java - Chapter 4 Pool Puzzle p.91" Watch "Head first Java - Chapter 4 Pool Puzzle p.91" New topic

Head first Java - Chapter 4 Pool Puzzle p.91

Taylor Street

Joined: Jul 10, 2010
Posts: 4
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;
return ivar * ( 5 - factor );

%java Puzzle4
result 543345
David Patrzeba

Joined: Jun 08, 2010
Posts: 14
First of all you should really use the code tags. You can use them by typing them:

Your code here

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

Joined: Jul 10, 2010
Posts: 4
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?

pete stein

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jul 10, 2010
Posts: 4
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

Joined: Jul 10, 2010
Posts: 4
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!
James Allen A.

Joined: Mar 10, 2013
Posts: 24
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?
I agree. Here's the link:
subject: Head first Java - Chapter 4 Pool Puzzle p.91
jQuery in Action, 3rd edition