• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java Book, Chapter 2

 
brian hague
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
brian hague
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic