import java.util.ArrayList; /** * This class contains methods for working with while loops and Die instances. * * @author Brad Richards */ public class LoopPractice { /** * This method rolls a Die the specified number of times, printing each * of the rolled values as it goes. * * @param d The Die to be rolled * @param numRolls The desired number of rolls */ public void rollNTimes(Die d, int numRolls) { int counter = 0; int rolledValue; while (counter < numRolls) { rolledValue = d.roll(); System.out.print(rolledValue+" "); // Use print and a space so they stay on one line counter = counter + 1; } System.out.println(); // Now move down to the next line to be polite } /** * Here's the method above, but rewritten using a for loop. This is better * style since it's a count-controlled loop. * * @param d The Die to be rolled * @param numRolls The desired number of rolls */ public void rollNTimes_for(Die d, int numRolls) { int rolledValue; for(int counter=0; counter < numRolls; counter=counter+1) { rolledValue = d.roll(); System.out.print(rolledValue+" "); // Use print and a space so they stay on one line } System.out.println(); // Now move down to the next line to be polite } /** * This method rolls a Die the specified number of times, then calculates * and returns the average of the rolled values. Note that the local * variables are both integers, but one of them is cast as a double before * doing the division so that we don't lose precision. * * @param d The Die to be rolled * @param numRolls The number of rolls to average * @return The average of the rolled values */ public double averageRoll(Die d, int numRolls) { int sum = 0; int counter; for(counter=0; counter < numRolls; counter=counter+1) { sum = sum + d.roll(); } return ((double)sum) / counter; } /** * This method rolls a Die the specified number of times, keeping track * of the largest and smallest values rolled and printing them after * making all of the rolls. * * @param d The Die to be rolled * @param numRolls The number of rolls to perform */ public void findMinAndMax(Die d, int numRolls) { /* * int min = d.roll(); * int max = min; */ int min = d.getNumSides()+1; // Larger than any possible rolled value int max = -1; for(int counter = 0; counter < numRolls; counter=counter+1) { int value = d.roll(); if (value < min) { min = value; } if (value > max) { max = value; } } System.out.println("After "+numRolls+" rolls, the min was "+min+ " and the max was "+max); } /** * This method rolls a Die until the largest and smallest possible values * have been rolled. It counts the number of rolls required and prints * it once the extreme values have both been rolled. * * @param d The Die to be rolled */ public void rollUntilExtremesFound(Die d) { boolean rolledMin = false; // Have we rolled a 1? boolean rolledMax = false; // Have we rolled largest yet? int counter = 0; while (!(rolledMin && rolledMax)) { // (rolledMin==false || rolledMax==false) int value = d.roll(); if (value == 1) { rolledMin = true; } if (value == d.getNumSides()) { rolledMax = true; } counter = counter + 1; } System.out.println("It took "+counter+" rolls until we saw both a 1 and a "+d.getNumSides()); } /** * This method rolls a Die until *all* possible values have been rolled. It * uses an ArrayList to record which values have been rolled, adding new values * to the list when we find values that aren't yet in the list. Once the list * gets to the appropriate size we stop and print the number of required rolls. * * @param d The Die to be rolled */ public void rollUntilAllValuesFound(Die d) { ArrayList rolledValues = new ArrayList(); int counter = 0; while (rolledValues.size() < d.getNumSides()) { int value = d.roll(); if (!rolledValues.contains(value)) { rolledValues.add(value); } counter = counter + 1; } System.out.println("It took "+counter+" rolls until all values were produced: "+rolledValues); } /** * Rolls a Die until the difference between two successive rolls is the value * specified by the user. When that condition is met, the method prints the * two values for the user. * * @param d The Die to be rolled * @param diff The desired difference between successive rolls */ public void rollUntilDifference(Die d, int diff) { int lastRoll = d.roll(); // This variable holds the "oldest" roll int currentRoll = d.roll(); // This holds the most recent roll while (Math.abs(lastRoll-currentRoll) != diff) { lastRoll = currentRoll; // Copy our most recent roll into "second place" currentRoll = d.roll(); // Make one new roll and store as most recent } System.out.println("Rolled a "+lastRoll+" and then a "+currentRoll); } }