1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mariulka [41]
3 years ago
13

1) Open the file DiceSimulation.java attached below. Create a new project on NetBeans called DiceSimulation. Copy the code from

the file into it. Note that 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 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 is included in the nested if-else-if statement.

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

Increment the number of times the dice were rolled

Note: To "roll" the dice, use the nextInt method of the random number generator to generate an integer between 1 and 6.

3) Compile and run you program. 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

1) Change the while loop to a do-while loop. Compile and run. You should get the same results.

2) Change the do-while loop to a for loop. Compile and run. You should get the same results.

//

Code Listing 4.1 (DiceSimulation.java)

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

}

}
Computers and Technology
1 answer:
trapecia [35]3 years ago
4 0

Answer:

As per regulations, I can only answer the code in while loop.

Explanation:

Code in JAVA is given below for while loop

Please read all the comments for better understanding of the code.

Every step is explained well in the code.

Note class name is DiceSimulation.

Code in JAVA (Using while loop)::

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

/**

* Following while loop will run until count < Number.

*/

while(count<NUMBER){

/**

* To generate random number in range 1 to 6,

* I have declared two integer variables named min and max

* and initialized to 1 and 6 respectively.

*/

int min=1,max=6;

/**

* An object of Random class named rand is created so that we can generate

* random number.

*/

Random rand=new Random();

 

/**

* Using following formula we get random number in range 1 to 6.

* Both variables i.e die1Value and die2Value are initialized

* with the formula given in each iteration.

*/

die1Value = rand.nextInt((max - min) + 1) + min;

die2Value = rand.nextInt((max - min) + 1) + min;

 

/**

* Now we are interested in cases where there is double i.e

* Both random generated numbers in die1Value and die2Value are same.

*/

if(die1Value==die2Value){

/**

* Now there are 6 possibilities. They are shown in Nested IF-ELSE-IF statements.

* As both values are same, I have taken die1Value to check if it is 1,2,3,4,5 or 6.

*/

if(die1Value==1){

/**

* If die1Value is 1 then we increment snakeEyes by 1.

* Similarly for others too we do the same.

*/

snakeEyes++;

}else if(die1Value==2){

twos++;

}else if(die1Value==3){

threes++;

}else if(die1Value==4){

fours++;

}else if(die1Value==5){

fives++;

}else if(die1Value==6){

sixes++;

}

}

/**

* In each iteration we increment count by 1.

*/

count++;

}//While loop ends 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.");

 

}//main ends

}//class ends

You might be interested in
Before you insert a page break, what should you do? select the font you want to use for the text that comes after the page break
Triss [41]
Put the insertion point where you want the page to break is what I would write.
7 0
3 years ago
For some reason my code is not printing in python. It doesn't give me an error is just not printing no thing shows up
svetoff [14.1K]

Answer:

Whats python? i might be able to help

Explanation:

5 0
3 years ago
Clickstream tracking tools collect data on customer activities at web sites (true/false)?
Galina-37 [17]

Clickstream tracking tools collect data on customer activities at websites- true.

<h3>what is the website?</h3>
  • A website, often known as a website, is a collection of web pages and associated material that is published on at least one web server and given a shared domain name.
  • The World Wide Web is the aggregate name for all publicly accessible websites.
  • A company's internal website for its workers is an example of a private website that can only be viewed via a private network.
  • Most websites focus on a single subject or objective, including news, education, business, entertainment, or social networking.
  • The navigation of the website, which frequently begins with a home page, is aided by hyperlinks between web pages.

To learn more about website, refer to the following link:

brainly.com/question/9060926

#SPJ4

3 0
2 years ago
3.3 Code Practice: Question 1
Bezzdna [24]

Answer:

day=int(input(“Enter today’s day numerically: ”))

if(day ==15 or day ==30):

print(“It’s payday!”)

if(day !=15 and day !=30):

print(“Sorry, not a payday.”)

Explanation:Good luck!

6 0
4 years ago
"Common knowledge" must always be credited or else it is considered plagiarism?<br> True<br> Fales
igomit [66]
Hi!

It depends on how close you have paraphrased it. If your "common knowledge" is very closely related to the resource, it would be considered plagiarism.
3 0
3 years ago
Other questions:
  • The key invention that enabled computers to go into every home and office is
    7·1 answer
  • System Architecture: Describe the system architecture. Specifically, be sure to address the corporate organization and culture,
    10·1 answer
  • While a hard drive is running, even a slight bump against the computer may cause the
    9·2 answers
  • Your bluetooth headset is waiting for another bluetooth device to locate its signal. what is this mode known as?'
    8·1 answer
  • What should be done if a system cannot boot from the hard drive?
    14·1 answer
  • Which is a form of cyber bullying??
    9·1 answer
  • Please choose the correct option please tell fast​
    9·1 answer
  • JUST NEED TO KNOW WHO ALL DOSE EDGINUITY
    12·2 answers
  • You have two identical print devices that are set up in a work room. Currently, the Windows print server has two printers config
    13·1 answer
  • Write a function DrivingCost with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar co
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!