Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Why Percentage Column is 0.00 ???

1. ## Why Percentage Column is 0.00 ???

Since the array is always passed by refrence, it should have been modified, but it did not.

This is my code:
-------------------------

import java.util.Random;

class DiceRolling
{
public void processDice()
{
int [] frequency = new int [ 13 ]; //frequency of every occurence of sum
double [] percentage = new double [ 13 ]; //percentage of occurence of the sum
final int TOTAL_ROLLS = 36000000; //times dice to be rolled for

for ( int roll = 1; roll <= TOTAL_ROLLS; roll++ ) //roll many times
{
int face1 = rollDie(); //roll dice 1
int face2 = rollDie(); //roll dice 2
int sum = face1 + face2; //summate the faces of both dice
++frequency [ sum ]; //increment the count of the occurence of sum
}//end for

calculatePercentage ( percentage, frequency, TOTAL_ROLLS ); //calculate percentage of each sum
displaySummary ( percentage, frequency, TOTAL_ROLLS ); //display the results in the tabular format

}//end method process Dice

private static int rollDie ()
{
Random randomNumbers = new Random (); //Random Numbers generator

int randomNumber = 1 + randomNumbers.nextInt ( 6 ); //Random Integer between 1 and 6 inclusive
return randomNumber; //return the generated random number

}//end method roll dice

private static void calculatePercentage ( double [] percentageArray, int [] frequencyArray, final int ROLLS )
{
for ( int counter = 2; counter <= 12; counter++ ) //store percentage of every sum in the array
percentageArray [ counter ] =
( double ) ( frequencyArray [ counter ] / ROLLS ) * 100; //percentage formula

}//end method calculate percentage

private static void displaySummary ( double [] percentageArray , int[] frequencyArray, int rolls )
{
System.out.println ("\n");
System.out.printf ("\n\tSUMMARY OF %d TIMES ROLLED DICE\n", rolls);
System.out.print (" ----------------------------------------------------\n\n");

System.out.printf ( "%7s%14s%15s\n\n", "DICE SUM", "FREQUENCY", "PERCENTAGE" );

//print summary of 12 sums
for ( int counter = 2; counter <= 12; counter++ )
System.out.printf ( "%7d%14d%15.2f\n", counter,
frequencyArray [ counter ], percentageArray [ counter ] );

}//end method display summary

}//end class DiceRolling

public class DiceRollingSimulation
{
public static void main ( String[] args )
{
DiceRolling aDice = new DiceRolling ();

}//end main

}//end class TwoDiceRollingSimulationtest.png  Reply With Quote

3. ## Re: Why Percentage Column is 0.00 ???

Too much code posted improperly. Difficult to read. Please post your code correctly per this link.  Reply With Quote

#### Tags for this Thread 