Create a die class to write a program that creates two dice
Create a die class to write a program that creates two dice.
A 20 sided and 6 sided die. Prompt a user to enter yes to roll or no to end the program. After the roll compare the results. If faces are equal, print \"Nice Roll\". Otherwise print
\"try again\". Repeat the program to allow the user to roll again or quit.
Solution
package com.amdocs.project;
import java.util.Scanner;
public class PairOfDice {
private int die1; // Number showing on the first die.
private int die2; // Number showing on the second die.
public PairOfDice() {
// Constructor. Rolls the dice, so that they initially
// show some random values.
roll(); // Call the roll() method to roll the dice.
}
public void roll() {
// Roll the dice by setting each of the dice to be
// a random number between 1 and 6.
// and second die between 1 and 20
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*20) + 1;
}
public int getDie1() {
// Return the number showing on the first die.
return die1;
}
public int getDie2() {
// Return the number showing on the second die.
return die2;
}
public boolean compare() {
// Return the boolean indicating weather the number on the dice are equal or not.
return die1==die2;
}
/*
*/
public static void main(String[] args) {
PairOfDice dice; // A variable that will refer to the dice.
int rollCount; // Number of times the dice have been rolled.
dice = new PairOfDice(); // Create the PairOfDice object.
/* Roll the dice */
Scanner sc;
sc=new Scanner(System.in);
String response=\"yes\";
while (!response.equals(\"no\")){
System.out.println(\"The dice come up \" + dice.getDie1()
+ \" and \" + dice.getDie2());
if (dice.compare()){
System.out.println(\"Nice Roll!\");
}else
{
System.out.println(\"Try again?\");
response=sc.next();
}
}
sc.close();
System.out.println(\"Over!\");
}
}

