Complete the ToDo items: package algs11; /** * CSC300 Program 3b: the file name
ID: 3712430 • Letter: C
Question
Complete the ToDo items:
package algs11;
/**
* CSC300 Program 3b: the file name must be PairOfDice
* Version 1.0
*
* Your Name goes here
* You class section goes here
*
* copy/paste the output in the space below
* ------------------------------------------------------
*
*
*
* --------------------------------------------------------
*
* This is a skeleton file for your homework. Edit the sections marked TODO.
*
* You must not change the declaration of any method.
* You may not use any other Java classes (e.g. ArrayLists, etc) without permission
* of the instructor.
* Do not change the main function
*/
import stdlib.StdOut;
import stdlib.StdRandom;
/*
* The class PairOfDice implement some basic functionality
* of a pair of dice. You may not add any methods to this class
* other than those indicated below.
*/
public class PairOfDice {
private int die1, die2;
public PairOfDice() { // constructor
roll();
}
public void roll() { // randomize the dice values
die1 = StdRandom.uniform(1, 7); // returns a random number from 1 to 6
die2 = StdRandom.uniform(1, 7);
}
public boolean isDoubles() { // determines if the two die values are the name
return die1 == die2;
}
public int sum() { // the the sum of the two die values
return die1 + die2;
}
// ToDo 1
// add an instance method that determines if the current dice sum is 7 or 11
// return true or false
// ToDo 2
// add an instance method that determines if both dice are odd.
// hint boolean
//------------------------------------- testing program ---------------------
/* experiment 1
*
* Q. how many rolls does it take (on the average) to roll a seven or eleven?
*
* first write a loop to count the number of rolls needed to roll a seven or eleven
* For example if the rolls were: 2 8 3 5 12 11, it took 6 rolls to get an 11 (this time)
* then add code to repeat that test 1,000,000 times.
* return: the average (number of rolls needed) of all the tests
* hint: nested loops, use the sevenOrEleven instance method
*/
public static double experimentOne( PairOfDice x) {
return -1.0; // ToDo 3
}
/*
* experiment 2
* Question: what is the likelihood of rolling odd-valued doubles?
* To answer this, complete this function which
* should roll the dice 1,000,000 times and
* determine and return the percentage of rolls that are doubles
* with an odd die value. ( 1,1) or (3,3) or (5,5)
*
* Hint: you will need to used instance methods of the PairOfDice class
*/
public static double experimentTwo( PairOfDice x) {
return -1.0; // ToDo 2:
}
// nothing to do here
public static void main(String[] args) {
PairOfDice myDice = new PairOfDice();
double rollsToGet7or11 = experimentOne(myDice);
double probOddDoubles = experimentTwo(myDice);
StdOut.format(" The average number of rolls to get a 7 or 11 was %5.3f ", rollsToGet7or11);
StdOut.format(" The experimental probablity of rolling odd doubles is %5.3f ", probOddDoubles);
}
}
Explanation / Answer
package algs11; /** * CSC300 Program 3b: the file name must be PairOfDice * Version 1.0 * * Your Name goes here * You class section goes here * * copy/paste the output in the space below * ------------------------------------------------------ * * * * -------------------------------------------------------- * * This is a skeleton file for your homework. Edit the sections marked TODO. * * You must not change the declaration of any method. * You may not use any other Java classes (e.g. ArrayLists, etc) without permission * of the instructor. * Do not change the main function */ import stdlib.StdOut; import stdlib.StdRandom; /* * The class PairOfDice implement some basic functionality * of a pair of dice. You may not add any methods to this class * other than those indicated below. */ public class PairOfDice { private int die1, die2; public PairOfDice() { // constructor roll(); } public void roll() { // randomize the dice values die1 = StdRandom.uniform(1, 7); // returns a random number from 1 to 6 die2 = StdRandom.uniform(1, 7); } public boolean isDoubles() { // determines if the two die values are the name return die1 == die2; } public int sum() { // the the sum of the two die values return die1 + die2; } // ToDo 1 // add an instance method that determines if the current dice sum is 7 or 11 // return true or false public boolean isSum7Or11() { int sum = die1 + die2; return sum == 7 || sum == 11; } // ToDo 2 // add an instance method that determines if both dice are odd. public boolean areBothOdd() { return die1 % 2 == 1 && die2 % 2 == 1; } // hint boolean //------------------------------------- testing program --------------------- /* experiment 1 * * Q. how many rolls does it take (on the average) to roll a seven or eleven? * * first write a loop to count the number of rolls needed to roll a seven or eleven * For example if the rolls were: 2 8 3 5 12 11, it took 6 rolls to get an 11 (this time) * then add code to repeat that test 1,000,000 times. * return: the average (number of rolls needed) of all the tests * hint: nested loops, use the sevenOrEleven instance method */ public static double experimentOne( PairOfDice x) { int count = 1000000; int rolls; double avg = 0; PairOfDice dice = new PairOfDice(); for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.