• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding questions  RSS feed

 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am making a program about the game of nim. I need to make two players, a human and a computer player. The following is the Player class:


If I take out Pile myPile = new Pile(), can I still use method upon it? like myPile.getSize(). I thought only object could do that.
About the player class, my question is should I assign something else to each player? A friend told me that I have to use an integer to keep track if it's a human player or computer player. So basically it's like this:


I don't know why I need that to track the players?
One last question, In the player class, there is the method play(), but in my Game class that I didn't post, it also has a method play(). In the end, in my Test class, I have Game g=new Game(), g.play(). Am I referring to the play method in the Game class?

I hope I have made my question clear. thanks.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's lots of ways to set a boolean value for isHuman.... you should decide how it is you're going to get that information (or if you need it or care or whatever), then probably use a setHuman() method that takes an int or a String, or whatever.

and yes, g.play() accesses the method in the class referenced by g. You'll need a reference to a Play object to use that method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheryl Scodario wrote:If I take out Pile myPile = new Pile(), can I still use method upon it?

No, that line (line 3 in the code you posted above) is the declaration of the variable. If you remove that whole line, then the variable won't exist anymore and you would get a compiler error in line 7 (the compiler will complain that it doesn't know what "myPile" there is).

Cheryl Scodario wrote:A friend told me that I have to use an integer to keep track if it's a human player or computer player.

Why an integer? Java also has the boolean type (true / false) which seems to be more suited for this.

Cheryl Scodario wrote:
One last question, In the player class, there is the method play(), but in my Game class that I didn't post, it also has a method play(). In the end, in my Test class, I have Game g=new Game(), g.play(). Am I referring to the play method in the Game class?

Yes, if you call it on a Game object, then you're referring to the play() method in the Game class.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use polymorphism. Create a class Player and create 2 classes, Human and Computer, which extend Player. Then you don't have to keep track of whether it is a human or a computer because they will contain the same methods.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:There's lots of ways to set a boolean value for isHuman.... . . .
But the if-else style doesn't read at all nicely. TryYou can use () if you wish, but they are not necessary.
You have already been told polymorphism is a far better way to do it.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines in code tags; they are difficult to read.
At the end of your first code block in your first post you say "equal sign". That isn't an equal sign, however much it looks like it. That = is an assignment operator. The equal sign in Java™ (and C, C++ and C#) looks like this: ==
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Cheryl Scodario wrote:If I take out Pile myPile = new Pile(), can I still use method upon it?

No, that line (line 3 in the code you posted above) is the declaration of the variable. If you remove that whole line, then the variable won't exist anymore and you would get a compiler error in line 7 (the compiler will complain that it doesn't know what "myPile" there is).


Hi Jesper, sorry that I didn't make myself clear. I meant that once I take out Pile myPile = new Pile(), I put in private Pile myPile. So Instead of constructing an object, I just declare it as a instance variable. One thing though, in this case, in Player class, I declared a variable type Pile, what does it exactly mean? Can this work? Can instance variables still exist even if there is no object in this class?
Another question is in "Pile myPile = new Pile()", myPile is an object reference. And bag is just a instance variable, so if I put myPile=bag, does it mean bag also points at the same object that myPile points? Thanks.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please avoid long lines in code tags; they are difficult to read.
At the end of your first code block in your first post you say "equal sign". That isn't an equal sign, however much it looks like it. That = is an assignment operator. The equal sign in Java™ (and C, C++ and C#) looks like this: ==


Oops! Thanks for this important reminder! I totally forgot! Now it starts to make sense. So if myPile=bag, then does it mean the instance variable bag point at the same object that myPile points at?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheryl Scodario wrote:Hi Jesper, sorry that I didn't make myself clear. I meant that once I take out Pile myPile = new Pile(), I put in private Pile myPile. So Instead of constructing an object, I just declare it as a instance variable.

So you mean that instead of this:

you would write this:

(Explaining it with code makes it exactly clear what you mean!). Yes, you can do that. Note that in the original code, you are also declaring an instance variable; in that regard, there is no difference. It's just that in the first case, you initialize the instance variable with a new Pile object, and in the second case, it will be initialized to null. And in the second case, the member variable is private.

Cheryl Scodario wrote:One thing though, in this case, in Player class, I declared a variable type Pile, what does it exactly mean? Can this work? Can instance variables still exist even if there is no object in this class?

Yes, if you don't initialize a member variable (of a non-primitive type) explicitly, it will be initialized to null (so, it would not be referring to any object).

Cheryl Scodario wrote:Another question is in "Pile myPile = new Pile()", myPile is an object reference. And bag is just a instance variable, so if I put myPile=bag, does it mean bag also points at the same object that myPile points? Thanks.

All variables of non-primitive types are references to objects; doesn't matter if they are instance variables or local variables. bag is not an instance variable, it's an argument (which works like a local variable). A statement like myPile = bag; will make myPile refer to the same object as that bag refers to - not the other way around, as you're saying.

Instance variables are variables declared at the level of the class (i.e., not declared inside a method). Each instance of the class (each object made from the class) will have its own copy of the instance variables of the class.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper, thanks! Everything makes perfect sense now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!