no arrays Dice rolling project Sample Console Output Roll the dice? 1y/) Rell 1:
ID: 3729374 • Letter: N
Question
no arrays
Dice rolling project Sample Console Output Roll the dice? 1y/) Rell 1: 2&5 Ro1l again? (y/n):Y Roll 2: 2.1 Roll again? (/n Y Roll 3: 4‘6 Roll again? (y/n) Ro11 4 &6 Box cars! Ro1l again? (y/n)Y Roll 5: 1&1 Snake eyes! Roll again? Y/n): n Operation If the user chooses to roll the dice, the application rolls two six-sided dice, displays the results of each, and asks if the user wants to roll again. Specifications . Create a public class named DiceRoller App that has the main method and uses the PairOfDice class to roll the dice each time the user responds 'y' to the request prompts. This app class should display special messages for "craps" (sum of both dice is T), "snake cyes (double 1's), and "box cars" (double 6's). For this application, assume that two six-sided dice are used.Explanation / Answer
import november.secondweek.Die;
public class PairOfDice {
// two Die reference
private Die dice1;
private Die dice2;
// constructor
public PairOfDice(){
//creating Die Objects
dice1 = new Die();
dice2 = new Die();
}
// set face values for dices
public void setFaceValueFirstDie(int faceValue){
dice1.setFaceValue(faceValue);
}
public void setFaceValueSecondDie(int faceValue){
dice2.setFaceValue(faceValue);
}
// get face values
public int getFaceValueFirstDie(){
return dice1.getFaceValue();
}
public int getFaceValueSecondDie(){
return dice2.getFaceValue();
}
// roll dice
public void roll(){
dice1.roll();
dice2.roll();
}
public int getSum(){
return dice1.getFaceValue()+dice2.getFaceValue();
}
}
#############
import java.util.Scanner;
public class DiceRoller {
public static void main(String[] args) {
System.out.println("Welcome to the Paradise Roller");
Scanner sc = new Scanner(System.in);
char op;
System.out.print("Roll the dice? (y/n): ");
op = sc.next().charAt(0);
PairOfDice dice = new PairOfDice();
int i = 1;
while( op == 'y') {
dice.roll();
System.out.println("Roll "+i+": "+dice.getFaceValueFirstDie()+" & "+dice.getFaceValueSecondDie());
if(dice.getSum() == 7)
System.out.println("Craps");
else if(dice.getFaceValueSecondDie() == dice.getFaceValueFirstDie() && dice.getFaceValueFirstDie() == 1)
System.out.println("Snake eyes");
else if(dice.getFaceValueSecondDie() == dice.getFaceValueFirstDie() && dice.getFaceValueFirstDie() == 6)
System.out.println("Box cars");
System.out.print("Roll the dice? (y/n): ");
op = sc.next().charAt(0);
i++;
}
}
/*
Sample run:
Welcome to the Paradise Roller
Roll the dice? (y/n): y
Roll 1: 5 & 1
Roll the dice? (y/n): y
Roll 2: 4 & 2
Roll the dice? (y/n): y
Roll 3: 2 & 6
Roll the dice? (y/n): y
Roll 4: 1 & 5
Roll the dice? (y/n): y
Roll 5: 2 & 3
Roll the dice? (y/n): y
Roll 6: 3 & 1
Roll the dice? (y/n): y
Roll 7: 1 & 4
Roll the dice? (y/n): y
Roll 8: 6 & 2
Roll the dice? (y/n): y
Roll 9: 6 & 1
Craps
Roll the dice? (y/n): y
Roll 10: 5 & 6
Roll the dice? (y/n): n
*/
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.