Hello.
I have made a program that rolls a pair of dice and then calculates the percentage of time a number has come up (see output on image). I am having a problem getting the percentages right...mostly because I calculate the average...how would I get the percent??? However, the main problem I have is breaking this program up into 3 classes (as per the assignment). I need to have 3 classes: MainDice(main), Die, and Dice. See the image to get a better understanding. I'll attach my code also:

public class MainDice {
   
      static final int NUMBER_OF_EXPERIMENTS = 1000;
   
      public static void main(String[] args) {
              // Find the average number of times a pair of dice has
              // to be rolled to get a given total.  Do this for
              // each possible total roll, 2 through 12, and print 
              // the results.
          double average;  // The average number of rolls to get a given total.
          System.out.println("Total On Dice     Percentage Rolled");
          System.out.println("-------------     -----------------------");
          for ( int dice = 2;  dice <= 12;  dice++ ) {
             average = getAverageRollCount( dice );
             System.out.print(dice);
             System.out.print("               ");
             System.out.println(average);
          }
      } 
      
   
      static double getAverageRollCount( int roll ) {
               // Find the average number of times a pair of dice must
               // be rolled to get a total of "roll".  Roll MUST be
               // one of the numbers 2, 3, ..., 12.
          int rollCountThisExperiment;  // Number of rolls in one experiment.
          int rollTotal;  // Total number of rolls in all the experiments.
          double averageRollCount;  // Average number of rolls per experiment.
          rollTotal = 0;
          for ( int i = 0;  i < NUMBER_OF_EXPERIMENTS;  i++ ) {
             rollCountThisExperiment = rollFor( roll );
             rollTotal += rollCountThisExperiment;
          }
          averageRollCount = ((double)rollTotal) / NUMBER_OF_EXPERIMENTS;
          return averageRollCount;
      }
      
   
     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;
      }
      
   }

Assignment:

if you roll the dice 10 times, and a 6 shows up twice, that's 20% 2/10 = 0.2
Divide the number of times a number shows up by the total number of rolls.

Thanks for the percent help. Now do you or anyone else know how to break up this program into 3 classes as stated in the image? I'm having a real tough time... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.