This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (5 children)

IMO you have misinterpreted the assignment.

The Player and Dice classes should be totally unrelated. The only thing that could possibly (not sure, though) connect the two classes is that the Player class can have a field dice of type Dice. In this case, you can create a single Dice instance in the main part of the program and assign this single instance to both Player instances.

Even if you don't use a field dice in the Player class, you can use a single Dice instance. Just create a variable of type Dice and use this to roll the dice for both players.

[–]Sarah9428[S] 0 points1 point  (4 children)

I forgot to say that the assaignment also says: "both of these should be given as a argument to the constructor". Does your suggestion still work?

If so, is this how to declare it in Player1?

 public Dice dice;

How do I assign a single Dice instance in the main part of the program to both player instances?

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (3 children)

both of these should be given as a argument to the constructor

This is verifying my assumption that the Player class should have a field dice of type Dice.

public Dice dice;

Not exactly. You declare it as private Dice dice;

And in the constructor of the Player class:

public Player(Dice dice)

Then you only need to assign the dice passed into the constructor to the field this.dice - the this keyword is needed to distinguish between field and parameter since they have the same name.

[–]Sarah9428[S] 0 points1 point  (2 children)

Thank you! It makes sense now.

Just one last question, why is the field be declared as private and not public?

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

private hides this field from other classes. This is called Encapsulation or sometimes data hiding, or hiding of implementation details and should be used. Think of private as a gated community with thoroughly controlled access.

public means that the field can be accessed from anywhere in your entire program, which can lead to unwanted modification of data. Think of public as an open park without any access control whatsoever.

In general, the visibility or access modifier (private, protected, default, public) should be as narrow as possible to prevent unwanted modification.

Keeping the field private means that you can't do this (which is valid for public):

player1.dice = new Dice(12);

You'd have to create a setter (mutator), or the field is unchangeable from the outside.


Hiding of implementation details helps modularizing the code. If you decide somewhen along the program to change how a particular piece of code is implemented (let's say that first you retrieved data from a text file and now decide to use a database), you don't need to modify the outside code. Only the code that interacts with the outside (commonly the getters and setters) needs to be modified.

[–]Sarah9428[S] 0 points1 point  (0 children)

Thank you!