Since the array is always passed by refrence, it should have been modified, but it did not.
Help me please.
Thanks in advance.
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");
//print headers
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 ();
aDice.processDice();
}//end main
}//end class TwoDiceRollingSimulationtest.png