please help on this As soon as possible Design and co de a program that simulate
ID: 3673831 • Letter: P
Question
please help on this As soon as possible
Design and co de a program that simulates the throwing o f a pair of dice (2 die). Each die has 6 sides, thus a number between 1 and 6 would represent a die. You want to show what happens when the dice are thrown 500 times. There are 11 possible throws (2,3,4,5,6,7,8,9,10,11,12) You will set up counters to show how many times 7 was thrown, how many times 2 was thrown and how many times doubles (same two die) was thrown. Print the counters with labels, such as "7 was thrown 100 times."Explanation / Answer
public class PairOfDice {
private int die1;
private int die2;
public PairOfDice() {
roll();
}
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public int getDie1() {
return die1;
}
public int getDie2() {
return die2;
}
public int getTotal() {
return die1 + die2;
}
}
public class Main {
public static void main(String[] args) {
PairOfDice dice;
int rollCount;
dice = new PairOfDice();
rollCount = 0;
do {
dice.roll();
System.out.println("The dice come up " + dice.getDie1()
+ " and " + dice.getDie2());
rollCount++;
} while (dice.getTotal() != 2);
System.out.println(" It took " + rollCount + " rolls to get a 2.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.