aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java Book, Chapter 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Book, Chapter 2" Watch "Head First Java Book, Chapter 2" New topic
Author

Head First Java Book, Chapter 2

brian hague
Greenhorn

Joined: Feb 22, 2009
Posts: 5
Hello,

this is my first post

I'm in the guessing Game in Chapter 2 of Head First Java (page 39).

Here's the code I've got entered:



now in the GuessGame class, where guessp1 = p1.number; it's not assigning the value from p1.number to guessp1. (also p2 and p3)... just look at 29-35

A workaround would of course be to return a value from p1.guess, but that's not the point of doing OO... is it?

Mind you this isn't exactly the way I would have coded it, I'm just new to java (just started about 1 hour ago...)... actually I worked with Java 1.02 in school, when it first came out... freakishly slow... the newer version is much better...

OO Java Greenhorn... old school procedural programmer
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
now in the GuessGame class, where guessp1 = p1.number; it's not assigning the value from p1.number to guessp1. (also p2 and p3)... just look at 29-35


I believe thats what it is doing. correct me if my understanding is wrong..


My Techie Blog - Java Questions
brian hague
Greenhorn

Joined: Feb 22, 2009
Posts: 5
it's supposed to give it the value of p1.number, but when it's compiled and run it takes a value of 0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Where did you initialise p1.number? If you don't initialise a number primitive field it will have 0 as a default value.
brian hague
Greenhorn

Joined: Feb 22, 2009
Posts: 5


In the Player class, number was initialized as int number = 0;, however right now it's not... . previous to p1.number, it calls p1.Guess(), which is supposed to assign a value to the variable 'number' on line 5 above.

right now the program will loop unless the first RNG also has a value zero, regardless of whether or not the Player.Guess() assigns a value to Player.number.

I've been reading ahead in the book, and it looks like int number should be in private, then have Guess return a number, or have a second function return a stored value... encapsulation. In this example, however, they don't use encapsulation since it hasn't been introduced yet.
brian hague
Greenhorn

Joined: Feb 22, 2009
Posts: 5
I've made some changes to the code... based on what I've learned from chapters 3-5... I would still like to understand why the variable was not able to assign a value from a public class variable...

GameLauncher.java


Player.java


GuessGame.java


hopefully this will help anyone else who is reading this book...



Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
But you haven't actually changed the functionality of the code? It is still taking Player.number and creating a random number in the Player.guess method. It doesn't matter if the instance variable is public and you use a void method, or the instance variable is private and you use a return method. You still get the same result, just your second example protects the instance variable.

I went through this chapter last week, and didn't experience your initial problem.
brian hague
Greenhorn

Joined: Feb 22, 2009
Posts: 5
That's why I'm posting this here... It's not doing what it should...

maybe a problem with my Java version?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40



Take a look at you guess() method again. The issue is that you accidently declared a local variable "number", instead of just assign the random number to the instance variable "number".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java Book, Chapter 2