Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another beginner question

 
Steve Grosz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, still going through my book, and getting into objects.......

There are three classes:
GameLauncher
GuessGame
Player

And we begin with:


So, I'm confused to why you'd have Player P1, if you're going to have a new Player object created with p1=new Player(); ??

 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the line "Player p1" says that you have a reference to a Player object. think of it as a chair, or one of the three podiums on "Jeopardy". The GuessGame class has three such spots, and they exist whenever you create a GuessGame object.

However, there is nobody standing at the podiums until you create a "new Player()" So the line

p1 = new Player();

is where the contestant walks in, and you say "OK, you go stand at the first podium".
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred - I guess you're assuming that the p1, p2 and p3 referenced in lines 7 - 9 are meant to be the same variables declared in lines 2 - 4 (P1, P2, P3) and that the case difference is just a typo.

If that's the case - Steve - you may want to edit your post. Java is case sensitive, so your code as it stands is confusing.
 
Steve Grosz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you then say:


Instead of breaking it up into 2 steps?

fred rosenberger wrote:the line "Player p1" says that you have a reference to a Player object. think of it as a chair, or one of the three podiums on "Jeopardy". The GuessGame class has three such spots, and they exist whenever you create a GuessGame object.

However, there is nobody standing at the podiums until you create a "new Player()" So the line

p1 = new Player();

is where the contestant walks in, and you say "OK, you go stand at the first podium".
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Grosz wrote:Couldn't you then say:


Instead of breaking it up into 2 steps?


You could - it depends on exactly how you use the code. Based on the small snippet of code, you could create a Guessgame object and then initialise it by calling the stargame method. To restart the game you simply call the starGame method again. If you initialised p1, p2 and p3 at the same time as you declared them, then you would have to create a new Guessgame object for each new game.
Of course this is just one possible reason for writing the code like that. There could be others. Does the book not explain why the code is the way it is ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic