in java Create a class named Die to store the data about each die. This class sh
ID: 3724967 • Letter: I
Question
in java
Create a class named Die to store the data about each die. This class should contain these constructors and methods: public Die() set the initial value of the die to zero public void roll() public int getValue()
Create a class named Dice to store two dice. This class should contain two instance variables of the Die type and these constructors and methods: public Dice() instantiate the Die instance variables public int getDie1Value () public int getDie2Value () public int getSum() get the sum of both dice public void roll() roll both dice public void printRoll() display result of roll
You can use the random method of the Math class to generate a random number for a die like this: value = (int) (Math.random() * 6) + 1;
When printing the results of the roll of the dice, display the value of each die and the total. In addition, display special messages for craps (sum of both dice is 7), snake eyes (double 1’s), and box cars (double 6’s).
Continue only if the user enters “y” or “Y” at the “Roll again?” prompt.
Explanation / Answer
Die.java
import java.util.Random;
public class Die {
//Declaring instance variables
private int initialValue;
//Zero argumented constructor
public Die() {
this.initialValue = 0;
}
//This method roll the die
public void roll() {
Random rand = new Random();
initialValue = rand.nextInt(6) + 1;
}
public int getValue() {
return initialValue;
}
}
________________
Dice.java
public class Dice {
//Declaring instance variables
private Die die1;
private Die die2;
//Zero argumented constructor
public Dice() {
die1=new Die();
die2=new Die();
}
public int getDie1Value()
{
return die1.getValue();
}
public int getDie2Value()
{
return die2.getValue();
}
//This method will return the sum of two dice values
public int getSum()
{
return getDie1Value()+getDie2Value();
}
//This method will roll the two dice
public void roll()
{
die1.roll();
die2.roll();
}
//This method will display the two dice value and its sum
public void printRoll()
{
int sum=getSum();
System.out.println("Die1:"+getDie1Value());
System.out.println("Die2:"+getDie2Value());
System.out.println("Sum:"+sum);
if(sum==7)
{
System.out.println("Craps Rolled");
}
else if(getDie1Value()==1 && getDie2Value()==1)
{
System.out.println("Snake Eyes");
}
else if(getDie1Value()==6 && getDie2Value()==6)
{
System.out.println("Box Cars");
}
}
}
________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating an Instance of Dice class
Dice d=new Dice();
while(true)
{
d.roll();
d.printRoll();
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to roll again(Y/N) ::");
char ch = sc.next(".").charAt(0);
if(ch=='Y'||ch=='y')
continue;
else
{
System.out.println(" :: Program Exit ::");
break;
}
}
}
}
________________
Output:
Die1:5
Die2:5
Sum:10
Do you want to roll again(Y/N) ::y
Die1:6
Die2:2
Sum:8
Do you want to roll again(Y/N) ::y
Die1:4
Die2:2
Sum:6
Do you want to roll again(Y/N) ::y
Die1:1
Die2:1
Sum:2
Snake Eyes
Do you want to roll again(Y/N) ::y
Die1:1
Die2:2
Sum:3
Do you want to roll again(Y/N) ::y
Die1:6
Die2:6
Sum:12
Box Cars
Do you want to roll again(Y/N) ::y
Die1:6
Die2:2
Sum:8
Do you want to roll again(Y/N) ::y
Die1:1
Die2:2
Sum:3
Do you want to roll again(Y/N) ::y
Die1:6
Die2:2
Sum:8
Do you want to roll again(Y/N) ::y
Die1:4
Die2:4
Sum:8
Do you want to roll again(Y/N) ::n
:: Program Exit ::
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.