New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.


Get the latest Java books
h t t p : / /w w w . j a v a c o f f e e b r e a k . c o m /

Java Coffee Break

Solution for
Programming Exercise 4.3


THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.

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. You can assume that the parameter is one of the possible totals: 2, 3, ..., 12. 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. I call the function rollFor:

       static int rollFor( int N ) {
                // Roll a pair of dice repeatedly until the total on the
                // two dice comes up to be N.  The number of rolls is returned.
           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;
       }

It's important to be clear about the contract of this subroutine. It is assumed that N is 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. If not, the subroutine will go into an infinite loop since the condition for continuing the loop, roll != N, will always be true. It is the responsibility of the caller of the function to make sure that the actual parameter in the function call satisfies the conditions of the contract.

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

    public class RollFor2 {
      
       /*  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 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()
       
       static int rollFor( int N ) {
                // Roll a pair of dice repeatedly until the total on the
                // two dice comes up to be N.  N MUST be one of the numbers
                // 2, 3, ..., 12.  (If not, this routine will go into an
                // infinite loop!).  The number of rolls is returned.
           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 ]