• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java - Chapter 5

 
Iwin Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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};
theDotCom.setLocationCells(locations);

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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

[0,1,2]
[1,2,3]
[2,3,4]
[3,4,5]
[4,5,6]
[5,6,7]

If you try to access index 3, you will get an ArrayIndexOutOfBoundsException.
 
Iwin Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Iwin Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic