wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Head First Java - Chapter 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java - Chapter 5" Watch "Head First Java - Chapter 5" New topic

Head First Java - Chapter 5

Iwin Huang

Joined: Mar 08, 2005
Posts: 4
Hi, I'm confused about the whole chapter, chapter 5. In this chapter we keep talking about making a location for it of (three consecutive cells on a single row of seven virtual cells) for the SimpleDotCom class.

However if you notice the code insertion of:

SimpleDotCom theDotCom = new SimpleDotCom();
int randomNum = (int) (Math.random() * 6);
System.out.println("Random number is " + randomNum);
int[] locations = {randomNum, randomNum + 1, randomNum + 2};

You always set the location of the randon numbers in locationCells [0], [1], and [2]. The array is and will only be 3 cells long not seven cells long which makes sense. You can prove this by insering the following line of code into the setLocationCells method of SimpleDotCom:

public void setLocationCells(int[] locs) {
locationCells = locs;
System.out.println("Location Cell [0} is " + locationCells[0]);
System.out.println("Location Cell [1] is " + locationCells[1]);
System.out.println("Location Cell [2} is " + locationCells[2]);

Cells 3, 4, 5, and 6 will never be populated. They will never exist.

Try adding:

System.out.println("Location Cell [3} is " + locationCells[3]);

You will get a compile error because cell [3] doses not exist.

I have spent many hours looking at this and getting very confused with this chapter. Can somebody help me? Thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You won't get a compile error, but you will get a runtime error. You're always constructing an array of size 3, which means the only valid index values are 0, 1, and 2. It will look like one of...


If you try to access index 3, you will get an ArrayIndexOutOfBoundsException.

James Carman, President<br />Carman Consulting, Inc.
Iwin Huang

Joined: Mar 08, 2005
Posts: 4
I understand that, but the chapter claims it's creating an array of size 7 to insert the three values (randomNum, randomNum+1, and random+2) into. I'm thinking the array never gets that big.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I don't have that book on hand, or I'd look into it. But, if that's what they're saying then they're wrong. The array is always of size 3. Are you sure you typed the code correctly? I've heard a lot of folks rave about that book and such a blatantly incorrect example would be surprising.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11257

i found my book. i think you're mis-understanding what they're doing.

they are not trying to make an array of 7 cells. what they are doing is saying "ok, we have this theoretical 7x7 array where the dots could go. we don't need to really create the whole thing. we know each dotcom is 3 cells long, so we only need to create an array of size three to store the three locations."

each SimpleDotCom only has to store the three cells it cares about, not all 7 of the row it's in.

did that make an sense?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Iwin Huang

Joined: Mar 08, 2005
Posts: 4
Hi Fred, thanks for your response. I took another look at the book and the bottom of page 96 pretty much confirms what you just said. I think the errata threw me for a loop where it talked about the array begins at 0 and ends at 6 so I must compute a random number between 0 and 4. I have to remember that all this was considered virtual. Thanks for everybody's input.
I agree. Here's the link: http://aspose.com/file-tools
subject: Head First Java - Chapter 5