Chapter 4 Lab Loops Lab Objectives Be able to use the provided Dice class to cre
ID: 3593231 • Letter: C
Question
Chapter 4 Lab
Loops
Lab Objectives
Be able to use the provided Dice class to create a dice object with 2 die attributes.
Complete the Dice class and added appropriate Accessor methods.
Be able to finish coding the provided DiceSimulation class, specifically the method called
rollDiceAndTabulate();
Introduction
There are 3 types of loops that can be coded in a program. The while-loop, the do-while loop, and the for-loop. The while-loop is used when you do not know how many times your loop will repeat. The do-while loop is used when you want to repeat the loop at least once. The for-loop is used when you know how many times you wish to repeat a loop.
In this lab, you will need to demonstrate the use of a while-loop, a do-while loop, and a for-loop to simulate rolling the dice 10,000 times. Each time the dice are rolled, the program must use an if-statement to decide if the dice roll resulted in two of the same number, and if so, will keep a counter.
Task #1 – Complete the Dice Class
In the domain class, Dice, add two accessor methods that will return the values of die1 and die2, respectively.
Task #2 – Use the while- loop
In the driver class, DiceSimulation, within the method rollDiceAndTabulateWhile(), do the following:
Create an instance of the Dice domain class
Create a while-loop that will keep simulating the roll of dice object just created.
Within the loop, check the value of the 2 die just rolled. If there are duplicates, count the dice into the appropriate counter. Make sure to use an if-statement to determine if there are any of these combinations of die values:
ones -- number of times double one is rolled
twos -- number of times double two is rolled
threes - number of times double three is rolled
fours - number of times double four is rolled
fives - number of times double five is rolled
sixes - number of times double six is rolled
Inside the rollDiceAndTabulateFor() method also call the clearCounters() method at the beginning, and the summarizeResults() method at the end. IMPORTANT: Prior to calling the summarizeResults(), print a title stating the type of loop being used for these results. Add some ******* to the title, to serve as separators between loop results.
Once this is complete, go to the main method and call the rollDiceAndTabulateFor() method.
Task #3 The do-while loop
Follow the same steps as in Task #1, but instead of using the while-loop, use the do-while loop. Complete these steps in the rollDiceAndTabulateDoWhile() method.
Make sure you get the similar results to Task #1
Task #4 The for-loop
Follow the same steps as in Task #1, but instead of using the while-loop, use the for- loop. Complete the steps in the rollDiceAndTabulateFor()
Make sure you get the similar results to Task #1 & 2.
Task #5 The clearCounters() method
Write a short method that will re-set all the counters of ones, twos, threes, all the way to sixes, to 0.
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 String toString()
{
return "You rolled a ..." + die1 + " and " + die2; }
}
/**
* 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.
*/
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 // 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
}
public static void rollDiceAndTabulateWhile()
{
//Complete task 2 here
}
public static void rollDiceAndTabulteDoWhile()
{
//Complete task 3 here
}
public static void rollDiceAndTabulteFor()
{
//Complete task 4 here
}
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.");
}
}
Need help completing this.
Explanation / Answer
/*The 2 accessor methods are added in this class as said:
getDie1()
getDie2()
*/
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;
}
//accessor method
public int getDie1() {
return die1;
}
//accessor method
public int getDie2() {
return die2;
}
public String toString() {
return "You rolled a ..." + die1 + " and " + die2;
}
}
/*
The three methods : rollDiceAndTabulateWhile(), rollDiceAndTabulteDoWhile(), rollDiceAndTabulteFor() are completed using 3 different loops, and the method for clearing the counters clearCounters() is also written.
*/
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 // 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("*******************************************");
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();
System.out.println("*******************************************");
}
public static void rollDiceAndTabulateWhile() {
// Complete task 2 here
clearCounters();
Dice dice = new Dice();
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() {
// Complete task 3 here
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=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() {
count=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.");
}
}
Sample Output:
*******************************************
While loop results:
You rolled double ones 274 out of 10000 rolls.
You rolled double twos 259 out of 10000 rolls.
You rolled double threes 269 out of 10000 rolls.
You rolled double fours 273 out of 10000 rolls.
You rolled double fives 276 out of 10000 rolls.
You rolled double sixes 284 out of 10000 rolls.
*******************************************
Do While loop results:
You rolled double ones 280 out of 10000 rolls.
You rolled double twos 275 out of 10000 rolls.
You rolled double threes 285 out of 10000 rolls.
You rolled double fours 288 out of 10000 rolls.
You rolled double fives 282 out of 10000 rolls.
You rolled double sixes 315 out of 10000 rolls.
*******************************************
For loop results:
You rolled double ones 259 out of 10000 rolls.
You rolled double twos 288 out of 10000 rolls.
You rolled double threes 273 out of 10000 rolls.
You rolled double fours 273 out of 10000 rolls.
You rolled double fives 266 out of 10000 rolls.
You rolled double sixes 274 out of 10000 rolls.
*******************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.