File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Head First Java - Chapter 4 puzzle question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java - Chapter 4 puzzle question" Watch "Head First Java - Chapter 4 puzzle question" New topic
Author

Head First Java - Chapter 4 puzzle question

William Wyatt
Greenhorn

Joined: Apr 24, 2006
Posts: 3
I'm trying to understand the pool puzzle problem on page 91 and hoping that someone can give me a hand. I can't figure out how the integer (ivar) gets any value assigned to it other than 0 and I can't understand how it works with 0 as the value. Below is a copy of the code.

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 );
}
}
}

p.s. This is my third head first book and I love them all.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Just looking at the Puzzle4b class by itself, you don't see the variable being assigned to anywhere. But look at Puzzle4, and you see

obs[x] = new Puzzle4b();
obs[x].ivar = y;

these two lines create a Puzzle4b instance, and set the value of ivar in that instance. Later, when obs[x].doStuff(x) is called, the Puzzle4b instance in obs[x] still has that nonzero value of ivar.


[Jess in Action][AskingGoodQuestions]
William Wyatt
Greenhorn

Joined: Apr 24, 2006
Posts: 3
Thank you very much, especially for the quick response. It seems so obvious now, don't know why I didn't see it before. Thanks again.
William Wyatt
Greenhorn

Joined: Apr 24, 2006
Posts: 3
One more quick question, in this section:

while ( x > 0 ) {
x = x - 1;
result = result + obs[x].doStuff(x);
}
System.out.println("result " + result);
}
}

Will the while loop run when x is equal to 0?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Nope.
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
it won't but x will receive the value 0 when you enter the loop at x=1, because of the line x=x-1.
 
Consider Paul's rocket mass heater.
 
subject: Head First Java - Chapter 4 puzzle question
 
Similar Threads
Head First page 92 Pool Puzzle
Head first Java - Chapter 4 Pool Puzzle p.91
Head first java exercise JVM problem
Head first programme
Help needen with Pool Puzzle excercise on page 91 Head First book...pleaseee