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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why Percentage Column is 0.00 ???

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    Lahore, Pakistan
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why Percentage Column is 0.00 ???

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why Percentage Column is 0.00 ???

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

Similar Threads

  1. Sum elements column by column
    By NallelyXIII in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 9th, 2013, 01:22 AM
  2. Percentage Array Help
    By Gra89 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 8th, 2012, 10:08 PM
  3. Calculationg the percentage.
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 25th, 2012, 08:25 AM
  4. It's not printing out the correct percentage...
    By JBow94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2010, 04:07 PM
  5. No Percentage?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 18th, 2009, 12:02 PM

Tags for this Thread