JAVA HELP Youve been hired by Cube Counters to write a Java

JAVA: HELP!!!

You\'ve been hired by Cube Counters to write a Java console application that repeatedly rolls two ten-sided dice and tabulates the results. To roll the dice, randomly generate two numbers between 1 and 10. Add the two numbers together and increment the counter for that sum. Possible sums range from 2 to 20 so store the counters in an integer array of 19 elements. Roll the dice 100,000 times. Format the output in three columns with the first column containing a sum, the second column containing the count, and the third column containing the percentage of that count over 100,00. Print the total rolls and percentage. These should be 100,000 and 100%, respectively. Format the counts with commas. Format the percentages with two decimal places.

Solution

package org.students;

import java.text.DecimalFormat;
import java.util.Random;

public class CubeCounters {

   public static void main(String[] args) {
      
       //Declaring variables
       int dice1,dice2,sum,count2=0,count3=0,count4=0, count5 = 0;
       int count6=0,count7=0,count8=0,count9=0,count10=0,count11=0,count12=0;
       int count13=0,count14=0,count15=0,count16=0,count17=0,count18=0,count19=0,count20=0;
       int totcount=0;
       double percentage=0.0,totPercentage=0.0;
      
       //Creating an integer array of size 19
       int arr[]=new int[19];
      
       //Creating an Random Class Object
       Random r = new Random();
      
       /* This for loop will continue to execute for 100000
       * meaning we are rolling two dice for 100000 times.
       */
       for(int i=1;i<=100000;i++)
       {
           //Rolling the first dice
           dice1= r.nextInt(10) + 1;
          
           //Rolling the Second Dice
           dice2= r.nextInt(10) + 1;
          
           //Sum of to dice
           sum=dice1+dice2;
          
           /* Based on the sum the corresponding case will get
           * executed and increment the counter of that sum
           */
           switch(sum)
           {
           case 2:
           arr[0]=++count2;
           break;
           case 3:
               arr[1]=++count3;
               break;
           case 4:
               arr[2]=++count4;
               break;
           case 5:
               arr[3]=++count5;
               break;
           case 6:
               arr[4]=++count6;
               break;
           case 7:
               arr[5]=++count7;
               break;
           case 8:
               arr[6]=++count8;
               break;
           case 9:
               arr[7]=++count9;
               break;
           case 10:
               arr[8]=++count10;
               break;
           case 11:
               arr[9]=++count11;
               break;
           case 12:
               arr[10]=++count12;
               break;
           case 13:
               arr[11]=++count13;
               break;
           case 14:
               arr[12]=++count14;
               break;
           case 15:
               arr[13]=++count15;
               break;
           case 16:
               arr[14]=++count16;
               break;
           case 17:
               arr[15]=++count17;
               break;
           case 18:
               arr[16]=++count18;
               break;
           case 19:
               arr[17]=++count19;
               break;
           case 20:
               arr[18]=++count20;
               break;

           }          
       }
       //Displaying the tabular form
       System.out.println(\"Sum\\tCount\\tPercentage\");
       for(int i=0;i<19;i++)
       {
           //Calculating the individual increment counter percentage
           percentage=((double)arr[i]*100)/100000;
          
           //calculating the total counter percentage
           totPercentage+=percentage;
          
           //Displaying the result
           System.out.printf(\"%d\\t%d,\\t%.2f\ \",(i+2),arr[i],percentage);
          
       }

       //This loop will calculates the total rolls
       for(int i=0;i<19;i++)
       {
           totcount+= arr[i];
       }
      
       //Displaying the total rolls
       System.out.println(\"Total Rolls :\"+totcount);
      
       //Displaying the total rolls percentage
       System.out.println(\"Total Percentage :\"+totPercentage+\"%\");
   }

}

_____________________________________

output:

Sum   Count   Percentage
2   998,   1.00
3   1997,   2.00
4   2954,   2.95
5   3996,   4.00
6   5025,   5.03
7   6086,   6.09
8   6889,   6.89
9   8037,   8.04
10   9154,   9.15
11   9975,   9.98
12   9047,   9.05
13   7877,   7.88
14   6984,   6.98
15   5974,   5.97
16   4880,   4.88
17   4009,   4.01
18   3083,   3.08
19   1992,   1.99
20   1043,   1.04
Total Rolls :100000
Total Percentage :100.0%

_______________Thank You

JAVA: HELP!!! You\'ve been hired by Cube Counters to write a Java console application that repeatedly rolls two ten-sided dice and tabulates the results. To rol
JAVA: HELP!!! You\'ve been hired by Cube Counters to write a Java console application that repeatedly rolls two ten-sided dice and tabulates the results. To rol
JAVA: HELP!!! You\'ve been hired by Cube Counters to write a Java console application that repeatedly rolls two ten-sided dice and tabulates the results. To rol

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site