• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java (2nd ed.) p. 107: Prep Code, Sharpen Your Pencil!  RSS feed

 
Takanori Masui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

I'm new to coding; only a bit of HTML in my history! I've made it to page 107 of Head First Java, and am stuck!

The section asks me to informally build the main method. Seems straightforward enough, but the second spec it lists is giving me serious trouble.

2. Make a location for [SimpleDotComObject] (three consecutive cells on a single row of seven virtual cells).


I have no idea how to go about doing this. Page 93 suggests to me that the seven virtual cells don't exist anywhere in the program. I'm not sure how this can be true, when a sentence later it says "the game...know[s] that the DotCom is hidden in three consecutive cells out of a possible seven". How can the game know it if it doesn't exist anywhere in the program?

Furthermore, how do I ensure that once I pick my first number, that the next two will be consecutive? Presumably the first number will be chosen using Math.random() * 4... But after that? I'm in the dark.

I'm really tempted to turn the page and look at Head First's code. But they insist I keep at it: "don't turn the page until you do this exercise!"

Any hints out there? Am I thinking about this in the wrong way? Did I miss something? Help would be much appreciated [:!



 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Takanori Masui wrote:
I have no idea how to go about doing this. Page 93 suggests to me that the seven virtual cells don't exist anywhere in the program. I'm not sure how this can be true, when a sentence later it says "the game...know[s] that the DotCom is hidden in three consecutive cells out of a possible seven". How can the game know it if it doesn't exist anywhere in the program?


An intuitive approach may involve having an array of seven indexes with four of them being blank, but the point it's making is that you don't actually need to allocate that much space. After all, you're only really interested in 3 of the 7 cells.

Takanori Masui wrote:
Furthermore, how do I ensure that once I pick my first number, that the next two will be consecutive? Presumably the first number will be chosen using Math.random() * 4... But after that? I'm in the dark.


Well, once you have the first integer, how would you mathematically generate two integers that are consecutive?
 
Takanori Masui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whew! That second question, when phrased that way, is a breeze. I think some of the brainfuzz from the first confusion floated into the second...

But as to the seven-virtual-cell question -- I understand that we're just interested in the DotComs that exist -- that is to say, we don't need an array to hold values for the empty cells. I'm just not sure about my alternatives. If, as a constraint, my "playing field" is seven integers long (0-7), and there's no array -- do I just write/say/code



or some such thing?

Is it that simple?

Thanks for your help!
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Takanori Masui wrote:Whew! That second question, when phrased that way, is a breeze. I think some of the brainfuzz from the first confusion floated into the second...

But as to the seven-virtual-cell question -- I understand that we're just interested in the DotComs that exist -- that is to say, we don't need an array to hold values for the empty cells. I'm just not sure about my alternatives. If, as a constraint, my "playing field" is seven integers long (0-7), and there's no array -- do I just write/say/code




Yes that would work. But remember that the allowed input is 0-6 (inclusive).

As for your Math.random() * 4, just remember to cast as an int.

Good luck.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid the > 7 bit wouldn't work. You have an out‑by‑one error. Write down all the indices of a 7‑element array, and you will see why.
Avoid messing around with Math.random(). If you need a “random” number, use this. In case you haven't come across imports before, you need to do it like this:They used to advise only having one Random instance in the whole application; I think that became less important since Java5.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Joel C has already pointed out the error about 7.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!