Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URGENT, MAKE BOTH INPUT AND OUTPUT APPEAR IN JOPTIONPANE 1/ import java.util.Random; //to use the random number generator public class DiceSimulation { static final int

URGENT,

MAKE BOTH INPUT AND OUTPUT APPEAR IN JOPTIONPANE

1/

import java.util.Random; //to use the random number generator public class DiceSimulation { static final int NUMBER = 10000; //the number of times to roll the dice //a random number generator used in simulating rolling a dice static Random generator = new Random(); static int die1Value; // number of spots on the first die static int die2Value; // number of spots on the second die static int count = 0; // number of times the dice were rolled static int ones = 0; // number of times double one is rolled static int twos = 0; // number of times double two is rolled static int threes = 0; // number of times double three is rolled static int fours = 0; // number of times double four is rolled static int fives = 0; // number of times double five is rolled static int sixes = 0; // number of times double six is rolled public static void main(String[] args) { //ENTER YOUR METHOD CALLS HERE System.out.println("While loop results: "); rollDiceAndTabulateWhile(); summarizeResults(); System.out.println("---------------------");

System.out.println("Do While loop results:"); rollDiceAndTabulteDoWhile(); summarizeResults(); System.out.println("---------------------"); System.out.println("For loop results:"); rollDiceAndTabulteFor(); summarizeResults(); } public static void rollDiceAndTabulateWhile() { // Complete task 2 here clearCounters(); Dice dice = new Dice(); // while (count <= NUMBER) { // myDice.rollDice(); /* if (die1Value == 1 && die2Value == 1) { ones += 1; } if (die1Value == 2 && */ while(count!= NUMBER) { count++; dice.rollDice(); die1Value = dice.getDie1(); die2Value=dice.getDie2(); if(die1Value!=die2Value) continue; else { if(die1Value==1) ones++; else if(die1Value==2) twos++; else if(die1Value==3) threes++; else if(die1Value==4) fours++; else if(die1Value==5) fives++; else sixes++; } } } public static void rollDiceAndTabulteDoWhile() { clearCounters(); Dice dice = new Dice(); do { count++; dice.rollDice(); die1Value=dice.getDie1(); die2Value=dice.getDie2(); if(die1Value!=die2Value) continue; else { if(die1Value==1) ones++; else if(die1Value==2) twos++; else if(die1Value==3) threes++; else if(die1Value==4) fours++; else if(die1Value==5) fives++; else sixes++; } } while(count!=NUMBER); }

public static void rollDiceAndTabulteFor() { // Complete task 4 here clearCounters(); Dice dice = new Dice(); /* for(count = 0; count <= NUMBER; count++) { myDice.rollDice(); } */ for(count=1;count<=NUMBER;count++) { dice.rollDice(); die1Value=dice.getDie1(); die2Value=dice.getDie2(); if(die1Value!=die2Value) continue; else { if(die1Value==1) ones++; else if(die1Value==2) twos++; else if(die1Value==3) threes++; else if(die1Value==4) fours++; else if(die1Value==5) fives++; else sixes++; } } }

public static void clearCounters() { ones = 0; twos = 0; threes = 0; fours = 0; fives = 0; sixes = 0; } public static void summarizeResults() { System.out.println("You rolled double ones " + ones + " out of " + NUMBER + " rolls."); System.out.println("You rolled double twos " + twos + " out of " + NUMBER + " rolls."); System.out.println("You rolled double threes " + threes + " out of " + NUMBER + " rolls."); System.out.println("You rolled double fours " + fours + " out of " + NUMBER + " rolls."); System.out.println("You rolled double fives " + fives + " out of " + NUMBER + " rolls."); System.out.println("You rolled double sixes " + sixes + " out of " + NUMBER + " rolls."); } }

2/

import java.util.Random; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/** * * @author cristy */ public class Dice { private int die1; private int die2; public Dice() { die1 = 1; die2 = 1; } public void rollDice() { Random myRan = new Random(); die1 = myRan.nextInt(6) + 1; die2 = myRan.nextInt(6) + 1; } public int getDie1() { return die1; }

public int getDie2() { return die2; } public String toString() { return "You rolled a ..." + die1 + " and " + die2; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions