[ Exercises | Chapter Index | Main Index ]

Solution for Programming Exercise 4.3


This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.


Exercise 4.3:

Write a function that simulates rolling a pair of dice until the total on the dice comes up to be a given number. The number that you are rolling for is a parameter to the function. The number of times you have to roll the dice is the return value of the function. The parameter should be one of the possible totals: 2, 3, ..., 12. The function should throw an IllegalArgumentException if this is not the case. Use your function in a program that computes and prints the number of rolls it takes to get snake eyes. (Snake eyes means that the total showing on the dice is 2.)


Discussion

The subroutine we have to write is very similar to the program from Exercise 3.1. The main difference is that instead of rolling until both dice come up 1, we roll until the total showing on the dice is equal to some specified value. That value is given by the parameter to the function. Of course, the first thing that the subroutine should do is check that the value of the parameter is in the range of possible rolls of a pair of dice; if not, it should throw an exception. Note that without this check, the subroutine would go into an infinite loop when the parameter is outside the range of possible values. I named the function rollFor:

public static int rollFor( int N ) {
    if ( N < 2 || N > 12 )
       throw new IllegalArgumentException("Impossible total for a pair of dice.");
    int die1, die2;  // Numbers between 1 and 6 representing the dice.
    int roll;        // Total showing on dice.
    int rollCt;      // Number of rolls made.
    rollCt = 0;
    do {
       die1 = (int)(Math.random()*6) + 1;
       die2 = (int)(Math.random()*6) + 1;
       roll = die1 + die2;
       rollCt++;
    } while ( roll != N );
    return rollCt;
}

In the actual program, I've added an appropriate Javadoc comment.

You should understand the contract of this subroutine. The parameter, N, is supposed to be one of the numbers that could possibly come up on a pair of dice. That is, N must be one of 2, 3, ..., or 12. The condition that N have a valid value is a precondition for the subroutine. If the caller of the function violates this precondition, the subroutine can't give any sort of correct answer, so it responds by throwing an exception.

The main() routine for this program is trivial. In fact, it could even be shortened to:

public static void main(String[] args) {
   System.out.println("It took " + rollFor(2) + " rolls to get snake eyes.");
}  // end main()

The Solution

/**
 * This program simulates rolling a pair of dice over and over until the
 * total showing on the two dice is 2.  It reports the number of rolls 
 * it took to get a 2.  (This was written to test the subroutine, rollFor.)
 */
public class RollFor2 {
  
   public static void main(String[] args) {
      int numberOfRolls;  // Number of rolls to get a 2.
      numberOfRolls = rollFor(2);
      System.out.println("It took " + numberOfRolls + " rolls to get snake eyes.");
   }  // end main()
   
   /**
    * Simulates rolling a pair of dice until a given total comes up.
    * Precondition:  The desired total is between 2 and 12, inclusive.
    * @param N the total that we want to get on the dice
    * @return the number of times the dice are rolled before the
    *    desired total occurs
    * @throws IllegalArgumentException if the parameter, N, is not a number
    *    that could possibly come up on a pair of dice
    */
   public static int rollFor( int N ) {
       if ( N < 2 || N > 12 )
          throw new IllegalArgumentException("Impossible total for a pair of dice.");
       int die1, die2;  // Numbers between 1 and 6 representing the dice.
       int roll;        // Total showing on dice.
       int rollCt;      // Number of rolls made.
       rollCt = 0;
       do {
          die1 = (int)(Math.random()*6) + 1;
          die2 = (int)(Math.random()*6) + 1;
          roll = die1 + die2;
          rollCt++;
       } while ( roll != N );
       return rollCt;
   }

}  // end class RollFor2

[ Exercises | Chapter Index | Main Index ]