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
If you omit the filename from a URL, the Apache web server displays a list of files and directories if the specified directory d
g100num [7]

Answer:

Default File

Explanation:

When a website is visited from a web browser without the explicit specification of the complete path of the file with the URL, the webserver will look for the file named index.html or index.php from the public_html files and render the contents of the file.

The name index. html or index.php is commonly used for the default page which is the page displayed to a visitor that does not specify a particular page for example (mysite/contact_us), so if the file path "contact_us" is omitted, the visitor will be taken to the "index" also known as the home page of "mysite".

6 0
3 years ago
How can a company that collects and uses private customer data ensure it will move forward in a sustainable culture of privacy?
Len [333]

Answer:

I think that it is c and if not pls take the point

back

4 0
2 years ago
Your essay is due tomorrow and you don't have time to write it. You decide to buy an essay online. You've paid for it, so it can
viva [34]

Answer:

false

even though you have paid for it , you still didn't write it by yourself, there for it will still be seen as plagiarism.......hope this helps

4 0
3 years ago
What is spam? a type of virus that spreads from computer to computer through a network connection a type of virus that targets p
Bas_tet [7]

Answer:

This is a pretty obvious answer.

An unwanted e-mail sent in bulk from people or organizations.

Explanation:

8 0
3 years ago
assume for arithmetic, load/store, and branch instructions, a processor has CPIs of 1, 12, and 5, respectively. Also assume that
KonstantinChe [14]

Answer:

1 PROCESSOR :

(1 × 2.56 × 10^9) + (12 × 1.28 × 10^9) + (5 × 2.56 × 10^8) / 2 GHz = 9.6 s

2 PROCESSORS :

(1×2.56×10^9)+(12×1.28×10^9)/0.7×2 + (5 × 2.56 × 10^8) / 2 GHz = 7.04 s

Speed -up is 1.36

4 PROCESSORS :

(1×2.56×10^9)+(12×1.28×10^9)/0.7×4 + (5 × 2.56 × 10^8) / 2 GHz = 3.84 s

Speed -up is 2.5

5 PROCESSORS :

(1×2.56×10^9)+(12×1.28×10^9)/0.7×8 + (5 × 2.56 × 10^8) / 2 GHz = 2.24 s

Speed -up is 4.29

Explanation:  

The following formula is used in this answer:

EXECUTION TIME = CLOCK CYCLES / CLOCK RATE

Execution Time is equal to the clock cycle per clock rate

7 0
3 years ago
Other questions:
  • You are describing the boot process to a friend and get to the step where the device loads the operating files into RAM, includi
    7·1 answer
  • If you inadvertently delete a file, you may be able to retrieve it from the A. Drop Box. B. System Restore. C. Restore File. D.
    15·1 answer
  • What do you think of explaining to young people, from high school, the legitimacy of copyright and the dangers they can run on t
    11·1 answer
  • C. you have already verified the routing table entries for r1, now execute the show run | section interface command to verify vl
    11·1 answer
  • Which data type structures best for insersion/ removal - Stack, linked list, queue?
    15·1 answer
  • A (an) block can arise from set ways of thinking.
    15·1 answer
  • Select the correct answer.
    8·1 answer
  • write a pay-raise program that requests a person's first name, last name, and current annual salary, and then displays the perso
    6·1 answer
  • True or False. A geosynchronous satellite changes its area in the sky each day.
    5·2 answers
  • "Automated Deployment" is one of the prerequisite for DevOps implementation.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!