Task #1 While loop 1. DiceSimulation.java is incomplete. Since there is a large
ID: 3650972 • Letter: T
Question
Task #1 While loop1. DiceSimulation.java
is incomplete. Since there is a large part of the program missing, the output will
be incorrect if you run DiceSimulation.java.
2. I have declared all the variables. You need to add code to simulate rolling the
dice and keeping track of the doubles. Convert the algorithm below to Java and
place it in the main method after the variable declarations, but before the output
statements. You will be using several control structures: a while loop and an ifelse-
if statement nested inside another if statement. Use the indenting of the
algorithm to help you decide what is included in the loop, what is included in
the if statement, and what is included in the nested if-else-if statement.
3. To
Explanation / Answer
Please rate...
Program Dice.java
=================================================
import java.util.Random;
public class Dice
{
private int spots; //the number of spots up on the die
private static Random generator;
//a random number generator use simulating rolling dice,
//shared by all dice, so that it will be as random as possible
//constructor creates a single die, inititally with no spots
public Dice()
{
//create an instance of the random
generator = new Random();
spots = 0;
}
//simulates rolling the die and stores the numbers rolled
public void roll()
{
//return 1,2,3,4,5, or 6
spots = generator.nextInt(6) + 1;
}
//return the value of the die
public int getSpots()
{
return spots;
}
}
=========================================================
Program DiceSimulation.java
=========================================================
public class DiceSimulation
{
public static void main(String [] args)
{
final int NUMBER = 10000; //the number of times the roll the dice
Dice die1 = new Dice(); //first die
Dice die2 = new Dice(); //second die
int die1Value; //the number of spots on the second die
int die2Value; //the number of spots on the first die
int count = 0; //number of times the dice were rolled
int snakeEye = 0; //number of times of snakes eyes is rolled
//number of times double two's, three's, four's, five's, six's
int twos = 0;
int threes = 0;
int fours = 0;
int fives = 0;
int sixes = 0;
//ENTER YOUR CODE FOR THE ALGORITHM HERE
for(count=0;count<=NUMBER;count++)
{
die1.roll();
int t1=die1.getSpots();
die2.roll();
int t2=die2.getSpots();
if(t1==t2)
{
if(t1==1)snakeEye++;
else if(t1==2)twos++;
else if(t1==3)threes++;
else if(t1==4)fours++;
else if(t1==5)fives++;
else if(t1==6)sixes++;
}
}
count--;
//output to console
System.out.println("You rolled snake eyes " + snakeEye
+ " out of " + count + " rolls.");
System.out.println("You rolled double two " + twos
+ " out of " + count + " rolls.");
System.out.println("You rolled double threes " + threes
+ " out of " + count + " rolls.");
System.out.println("You rolled double fours " + fours
+ " out of " + count + " rolls.");
System.out.println("You rolled double fives " + fives
+ " out of " + count + " rolls.");
System.out.println("You rolled double sixes " + sixes
+ " out of " + count + " rolls.");
}
}
========================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.