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

Another beginner question

Steve Grosz
Greenhorn

Joined: Jan 13, 2011
Posts: 7
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

Joined: Oct 02, 2003
Posts: 11230
    
  16

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".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
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.


Joanne
Steve Grosz
Greenhorn

Joined: Jan 13, 2011
Posts: 7
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

Joined: Aug 05, 2005
Posts: 3446
    
  12
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 ?
 
wood burning stoves
 
subject: Another beginner question