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 Need Help understanding example code:  HeadFirst Java 2nd Ed. (GuessGame) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help understanding example code:  HeadFirst Java 2nd Ed. (GuessGame)" Watch "Need Help understanding example code:  HeadFirst Java 2nd Ed. (GuessGame)" New topic
Author

Need Help understanding example code: HeadFirst Java 2nd Ed. (GuessGame)

Gavin Ralston
Greenhorn

Joined: May 24, 2008
Posts: 3
Okay, this has been posted a few times before, and I'm seeing some answers, but I'm really looking to understand *why* this doesn't work.

I'm pretty sure I busted this code out a year or so ago and it worked fine. Now I'm having all sorts of interesting dilemmas (or probably one interesting dilemma manifesting itself a few times).

Three Players should randomly guess a randomly generated target numbers. Rounds continue to be played til at least one gets it right.

I'm posting the code with my comments, which include what I'm trying to understand and what I think might be happening.






[ May 24, 2008: Message edited by: Gavin Ralston ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17




You have stumbled upon an issue of scope.

Inside the method, you've declared a completely different variable, also called number. This is the variable that you are giving the "guess" value, and when your method has finished and goes out of existence, so does this value. All you have left to access is your instance variable, which was never given a value at all, and which happens to default to zero.

So your game should be over pretty quickly if the number to guess is 0, since all the players get it right the first time!

There are a couple of things you could experiment with:

1) inside your method, instead of saying "int number", try using "this.number"
this refers to "the object to which I belong". Since each player has their own guess() method, then that method would know to look for a variable inside that particular instance of the class.
2) remove the type declaration inside your method. That is to say, use

instead of

[ May 24, 2008: Message edited by: Katrina Owen ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Yep, that's shadowing: there's a local variable inside the method which records the guess; it disappears immediately after the method is called, and the "number" member variable is never set to any value.

The fix is incredibly simple. Your method says

int number = (int) (Math.random() * 10);

which is basically shorthand for

int number; // Declare a new variable named "number"
number = (int) (Math.random() * 10); // Give the local "number" a value

instead you just want to write

number = (int) (Math.random() * 10); // Give the member "number" a value

Removing those three little character "int" at the beginning of the line makes all the difference!


[Jess in Action][AskingGoodQuestions]
Gavin Ralston
Greenhorn

Joined: May 24, 2008
Posts: 3
Thanks to both of you for catching that! It worked fine.

It didn't matter how many times I looked at the guess() method, I wasn't going to see I was declaring a totally new variable there. Heck, I even heavily commented the section and *still* couldn't see the insidious "int"

I think my confusion regarding the "if guessp1 == true" tests was more to do with me continually recompiling the Player.java file. I may have never actually compiled a version where I assigned a value to 'number' and thus didn't have a chance of lucking out of the loop sing targetNumber would never equal null.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help understanding example code: HeadFirst Java 2nd Ed. (GuessGame)