Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head first Java - Chapter 4 Pool Puzzle p.91

 
Taylor Street
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
David Patrzeba
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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!
 
James Allen A.
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic