Reputation: 21
I am trying to use the Die class when writing a PairOfDice class. Here is the Die class:
public static class Die {
private final int MAX = 6;
private int faceValue;
public Die()
{
faceValue = 1;
}
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
public void setFaceValue(int value)
{
if(value > 0 && value <= MAX)
faceValue = value;
}
public int getFaceValue()
{
return faceValue;
}
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
Now what I am trying to do is to use this class to write a similar class called PairOfDice. I want similar methods of rolling the dice, setting the face value, and so on. However I have never done this before so I'm not sure how to approach this. Here is what I have so far:
public static class PairOfDice {
Die die1 = new Die();
Die die2 = new Die();
public PairOfDice()
{
????
}
public int rollPair()
{
????
}
}
I'm not sure how to appropriately use these objects. Please keep in mind I am a beginner in java/programming.
Upvotes: 2
Views: 77
Reputation: 201447
You don't appear to need anything in the constructor, you've initialized the die fields where you declared them. Just call them in your method. Something like,
public int rollPair()
{
return die1.roll() + die2.roll();
}
Upvotes: 1