Modify the program again so that it prints the highest and lowest number of atte
ID: 3533319 • Letter: M
Question
Modify the program again so that it prints the highest and lowest number of attempts that are required to match any initial roll.
Check _____
Finally, modify the program once more so that it also prints the 2 initial rolls that required the highest and lowest number of attempts to be matched, as shown here:
Most attempts: 37 to match a 12
package Dominoes2;
import java.util.Random;
/**
* A class to represent a standard 6-sided die
*/
class Die // die is the singular of "dice"
{
// random number generator object
private static Random r = new Random();
/**
* Roll one die.
*
* @return a random integer from 1 to 6
*/
public int roll()
{
return r.nextInt(6) + 1;
}
}
/**
* Plays the game. I.e., rolls the dice to get the initial roll and counts
* number of additional rolls required to match it. Repeats as long as the user
* wants to play another game
*/
class MatchGame2
{
// instance var's are Die objects
Die d1 = new Die(); // create pair of dice
Die d2 = new Die();
// This method just aligns the output...
private static String format(int number)
{
if (number <= 9) // if single-digit number...
{
return " " + number; // ...pad with one leading space
}
else // 2-digit number...
{
return "" + number; // ...no padding
}
}
/**
* Plays the game. I.e., rolls the dice to get the initial roll and counts
* number of additional rolls required to match it. Repeats as long as the
* user wants to play another game
*/
public void play()
{
int initialRoll = 0; // total of first roll of two dice
int rollCount = 0 ; // counts number of rolls it takes to match the initial roll
int gameNumber = 0; // counts number of games played
System.out.println();
while (( initialRoll !=2) && (rollCount !=1 )) // loop forever! initialRoll != 2
{
//gameNumber = 0 gameNumber <= 10
gameNumber++; // increment # of games played
// get the initial roll
initialRoll = d1.roll() + d2.roll();
// roll dice again to try to match initial roll
int currentRoll = d1.roll() + d2.roll();
// initialize count of number of rolls needed to match
rollCount = 1;
// repeat as long as the initial roll is not matched
// DO NOT MODIFY THIS LOOP! ONLY THE OUTER ONE!
while (currentRoll != initialRoll) // while NOT matched...
{
// ...roll 'em again!
currentRoll = d1.roll() + d2.roll();
// ...increment number of rolls made
rollCount++;
}
// Loop postcondition: initialRoll has been matched
// print stats
System.out.println("Trial #" + format(gameNumber)
+ " Initial roll = " + format(initialRoll)
+ " Matched in " + format(rollCount)
+ " rolls.");
}
}
}
public class GallopingDominoes2
{
public static void main(String[] args)
{
MatchGame2 fun = new MatchGame2();
fun.play();
}
}
Explanation / Answer
package Dominoes2; import java.util.Random; /** * A class to represent a standard 6-sided die */ class Die // die is the singular of "dice" { // random number generator object private static Random r = new Random(); /** * Roll one die. * * @return a random integer from 1 to 6 */ public int roll() { return r.nextInt(6) + 1; } } /** * Plays the game. I.e., rolls the dice to get the initial roll and counts * number of additional rolls required to match it. Repeats as long as the user * wants to play another game */ class MatchGame2 { // instance var's are Die objects Die d1 = new Die(); // create pair of dice Die d2 = new Die(); // This method just aligns the output... private static String format(int number) { if (numberRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.