In: Computer Science
JAVA :
Many games are played with dice, known as a die in the singular. A die is a six-sided cube whose sides are labeled with one to six dots. The side that faces up indicates a die's face value.
Design and implement a Java class Die that represents one 6-sided die. You should be able to roll the die and discover its upper face. Thus, give the class the method int roll, which returns a random integer ranging from 1 to 6, and the method int getFaceValue, which returns the current face value of the die (last value rolled). Note a call to roll will return the same value as a subsequent call to getFaceValue.
Often, games depend on the roll of two dice. Using your class Die, design and implement a Java class TwoDice with an aggregation ("has-a") relationship to the Die class. An object of TwoDice represents two six-sided dice that are rolled together. Include at least the following TwoDice methods: int rollDice, int getFaceValue1, int getFaceValue2, int getValueOfDice (returns the sum of the two dice values), boolean isMatchingPair, boolean isSnakeEyes, andString toString.
Demonstrate your Die and TwoDice classes using the following test client:
public class Main
{
public static void main(String[] args)
{
Die dieOne = new Die();
Die dieTwo = new Die();
Die dieThree = new
Die();
System.out.println("Demonstrating the Die class:");
System.out.println("Rolling
dieOne: " + dieOne.roll());
System.out.println("Rolling
dieTwo: " + dieTwo.roll());
System.out.println("Rolling
dieThree: " + dieThree.roll());
TwoDice dice = new
TwoDice();
System.out.println("Demonstrating the TwoDice class: ");
System.out.println("Rolling
dice: [" + dice.getFaceValue1()
+ ", " + dice.getFaceValue2() + "]");
dice.rollDice();
System.out.println("Rolling
dice: [" + dice.getFaceValue1()
+ ", " + dice.getFaceValue2() + "]");
dice.rollDice();
System.out.println("Rolling
dice: [" + dice.getFaceValue1()
+ ", " + dice.getFaceValue2() + "]");
}
}
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== import java.util.Random; public class Die { public static final int SIDES = 6; private int faceValue; public Die() { faceValue = 0; } public int roll() { Random random = new Random(); faceValue =1 + random.nextInt(SIDES); return faceValue; } public int getFaceValue() { return faceValue; } }
import java.util.Random; public class Die { public static final int SIDES = 6; private int faceValue; public Die() { faceValue = 0; } public int roll() { Random random = new Random(); faceValue =1 + random.nextInt(SIDES); return faceValue; } public int getFaceValue() { return faceValue; } }
import java.util.Random; public class Die { public static final int SIDES = 6; private int faceValue; public Die() { faceValue = 0; } public int roll() { Random random = new Random(); faceValue =1 + random.nextInt(SIDES); return faceValue; } public int getFaceValue() { return faceValue; } }
==================================================================
public class TwoDice { private Die diceOne; private Die diceTwo; public TwoDice() { diceOne = new Die(); diceTwo = new Die(); } public String getFaceValue1() { return String.valueOf(diceOne.getFaceValue()); } public String getFaceValue2() { return String.valueOf(diceTwo.getFaceValue()); } public void rollDice() { diceOne.roll(); diceTwo.roll(); } }
===================================================================
public class Main { public static void main(String[] args) { Die dieOne = new Die(); Die dieTwo = new Die(); Die dieThree = new Die(); System.out.println("Demonstrating the Die class:"); System.out.println("Rolling dieOne: " + dieOne.roll()); System.out.println("Rolling dieTwo: " + dieTwo.roll()); System.out.println("Rolling dieThree: " + dieThree.roll()); TwoDice dice = new TwoDice(); System.out.println("Demonstrating the TwoDice class: "); System.out.println("Rolling dice: [" + dice.getFaceValue1() + ", " + dice.getFaceValue2() + "]"); dice.rollDice(); System.out.println("Rolling dice: [" + dice.getFaceValue1() + ", " + dice.getFaceValue2() + "]"); dice.rollDice(); System.out.println("Rolling dice: [" + dice.getFaceValue1() + ", " + dice.getFaceValue2() + "]"); } }
=============================================================