Question

In: Computer Science

Task #1 The while Loop (8 pts) 1. Copy the file DiceSimulation.java as directed by your...

Task #1 The while Loop (8 pts) 1. Copy the file DiceSimulation.java as directed by your instructor. Correct syntax errors if any, and improve programming style when necessary (indents, newlines, etc.). 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. We 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 into Java code 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 if-else-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 isincluded in the nested if-else-if statement. 3. To “roll” the dice, use the nextInt method of the random number generator to generate an integer from 1 to 6. Repeat while the number of dice rolls are less than the number of times the dice should be rolled. Get the value of the first die by “rolling” the first die Get the value of the second die by “rolling” the second die If the value of the first die is the same as the value of the second die If value of first die is 1 Increment the number of times snake eyes were rolled Else if value of the first die is 2 Increment the number of times twos were rolled Else if value of the first die is 3 Increment the number of times threes were rolled Else if value of the first die is 4 Increment the number of times fours were rolled Else if value of the first die is 5 Increment the number of times fives were rolled Else if value of the first die is 6 Increment the number of times sixes were rolled Page 3 of 3 Increment the number of times the dice were rolled 4. Compile and run. You should get numbers that are somewhat close to 278 for each of the different pairs of doubles. Run it several times. You should get different results than the first time, but again it should be somewhat close to 278. Task #2 Using Other Types of Loops: do-while (4 pts) 1. Change the while loop to a do-while loop. 2. Make other necessary changes to save your work as new file named DiceSimulation_Do.java. 3. Compile and run. You should get the same results as at Task #1. Task #3 Using Other Types of Loops: for (4 pts) 1. Change the do-while loop to a for loop. 2. Make other necessary changes to save your work as new file named DiceSimulation_For.java. 3. Compile and run. You should get the same results as at Task #1.

import java.util.Random;   // Needed for the Random class

/**
   This class simulates rolling a pair of dice 10,000 times
   and counts the number of times doubles of are rolled for
   each different pair of doubles.
*/

public class DiceSimulation
{
   public static void main(String[] args)
   {
      final int NUMBER = 10000;  // Number of dice rolls

      // A random number generator used in
      // simulating the rolling of dice
      Random generator = new Random();

      int die1Value;       // Value of the first die
      int die2Value;       // Value of the second die
      int count = 0;       // Total number of dice rolls
      int snakeEyes = 0;   // Number of snake eyes rolls
      int twos = 0;        // Number of double two rolls
      int threes = 0;      // Number of double three rolls
      int fours = 0;       // Number of double four rolls
      int fives = 0;       // Number of double five rolls
      int sixes = 0;       // Number of double six rolls

      // TASK #1 Enter your code for the algorithm here

      // Display the results
      System.out.println ("You rolled snake eyes " +
                          snakeEyes + " out of " +
                          count + " rolls.");
      System.out.println ("You rolled double twos " +
                          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.");
   }
}

Solutions

Expert Solution

Hi, please find the code below. There are three java files as mentioned in the given problem.

File- DiceSimulation.java

//-------------------------- DiceSimulation.java START ---------------------------------

import java.util.Random;   // Needed for the Random class

/**

   This class simulates rolling a pair of dice 10,000 times

   and counts the number of times doubles of are rolled for

   each different pair of doubles.

*/

public class DiceSimulation

{

   public static void main(String[] args)

   {

        final int NUMBER = 10000;  // Number of dice rolls

        // A random number generator used in

        // simulating the rolling of dice

        Random generator = new Random();

        int die1Value;       // Value of the first die

        int die2Value;       // Value of the second die

        int count = 0;       // Total number of dice rolls

        int snakeEyes = 0;   // Number of snake eyes rolls

        int twos = 0;        // Number of double two rolls

        int threes = 0;      // Number of double three rolls

        int fours = 0;       // Number of double four rolls

        int fives = 0;       // Number of double five rolls

        int sixes = 0;       // Number of double six rolls

      // TASK #1 Enter your code for the algorithm here

        while(count<NUMBER){

            die1Value = generator.nextInt(6)+1;

            die2Value = generator.nextInt(6)+1;

            if(die1Value == die2Value){

                if(die1Value == 1)

                    snakeEyes++;

                else if(die1Value == 2)

                    twos++;

                else if(die1Value == 3)

                    threes++;

                else if(die1Value == 4)

                    fours++;

                else if(die1Value == 5)

                    fives++;

                else

                    sixes++;

            

            }

            count++;

        }

      // Display the results

      System.out.println ("You rolled snake eyes " +

                          snakeEyes + " out of " +

                          count + " rolls.");

      System.out.println ("You rolled double twos " +

                          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.");

   }

}

//-------------------------- DiceSimulation.java END ---------------------------------

File - DiceSimulation_Do.java

//-------------------------- DiceSimulation_Do.java START ---------------------------------

import java.util.Random;   // Needed for the Random class

/**

   This class simulates rolling a pair of dice 10,000 times

   and counts the number of times doubles of are rolled for

   each different pair of doubles.

*/


public class DiceSimulation_Do

{

   public static void main(String[] args)

   {

        final int NUMBER = 10000;  // Number of dice rolls

        // A random number generator used in

        // simulating the rolling of dice

        Random generator = new Random();

        int die1Value;       // Value of the first die

        int die2Value;       // Value of the second die

        int count = 0;       // Total number of dice rolls

        int snakeEyes = 0;   // Number of snake eyes rolls

        int twos = 0;        // Number of double two rolls

        int threes = 0;      // Number of double three rolls

        int fours = 0;       // Number of double four rolls

        int fives = 0;       // Number of double five rolls

        int sixes = 0;       // Number of double six rolls

      // TASK #1 Enter your code for the algorithm here

        do{

            die1Value = generator.nextInt(6)+1;

            die2Value = generator.nextInt(6)+1;

            if(die1Value == die2Value){

                if(die1Value == 1)

                    snakeEyes++;

                else if(die1Value == 2)

                    twos++;

                else if(die1Value == 3)

                    threes++;

                else if(die1Value == 4)

                    fours++;

                else if(die1Value == 5)

                    fives++;

                else

                    sixes++;

            

            }

            count++;

        }

        while(count<NUMBER);

      

      // Display the results

      System.out.println ("You rolled snake eyes " +

                          snakeEyes + " out of " +

                          count + " rolls.");

      System.out.println ("You rolled double twos " +

                          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.");

   }

}

//-------------------------- DiceSimulation_Do.java END ---------------------------------

File- DiceSimulation_For.java

//-------------------------- DiceSimulation_For.java START ---------------------------------

import java.util.Random;   // Needed for the Random class

/**

   This class simulates rolling a pair of dice 10,000 times

   and counts the number of times doubles of are rolled for

   each different pair of doubles.

*/

public class DiceSimulation_For

{

   public static void main(String[] args)

   {

        final int NUMBER = 10000;  // Number of dice rolls

        // A random number generator used in

        // simulating the rolling of dice

        Random generator = new Random();

        int die1Value;       // Value of the first die

        int die2Value;       // Value of the second die

        int count = 0;       // Total number of dice rolls

        int snakeEyes = 0;   // Number of snake eyes rolls

        int twos = 0;        // Number of double two rolls

        int threes = 0;      // Number of double three rolls

        int fours = 0;       // Number of double four rolls

        int fives = 0;       // Number of double five rolls

        int sixes = 0;       // Number of double six rolls

      // TASK #1 Enter your code for the algorithm here

        for(count=0;count<NUMBER;count++)

        {

            die1Value = generator.nextInt(6)+1;

            die2Value = generator.nextInt(6)+1;

            if(die1Value == die2Value){

                if(die1Value == 1)

                    snakeEyes++;

                else if(die1Value == 2)

                    twos++;

                else if(die1Value == 3)

                    threes++;

                else if(die1Value == 4)

                    fours++;

                else if(die1Value == 5)

                    fives++;

                else

                    sixes++;

          }

        }

        

      

      // Display the results

      System.out.println ("You rolled snake eyes " +

                          snakeEyes + " out of " +

                          count + " rolls.");

      System.out.println ("You rolled double twos " +

                          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.");

   }

}

//-------------------------- DiceSimulation_For.java END ---------------------------------


Related Solutions

Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
in C++ use loopDW.cpp below Do While Loop Exercise Get a copy of the loopDW.cpp. Add...
in C++ use loopDW.cpp below Do While Loop Exercise Get a copy of the loopDW.cpp. Add a do-while loop to complete the program. The do-while loop will make sure that a valid mark will be obtained before further process. (hint: a event-controlled do-while loop is needed.) Compile and run the program. Use input values: -10, 30, 59, 70, 75, 96, and 108. Show the output to your lab instructor. #include <iostream> using namespace std; int main() { int mark; char...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Use a while loop to read each line in the file modified_etc_passwd directly and process each...
Use a while loop to read each line in the file modified_etc_passwd directly and process each line as follows:  Use a combination of the echo and cut commands to assign the contents of the home directory field of the current record to a variable called homedir  Use a combination of the echo and cut commands to assign the contents of the username field to a variable called username  If homedir exists as a directory o Use a...
a) Submit a copy of your dataset along with a file that contains your answers to...
a) Submit a copy of your dataset along with a file that contains your answers to all of the following questions. b) What the mean and Standard Deviation (SD) of the Close column in your data set? c) If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed at less than the mean for that year? Hint: You do not want to calculate the mean to...
1) Write a denotational semantics for do-while loop
1) Write a denotational semantics for do-while loop
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT