Results 1 to 5 of 5

Thread: assignment help

  1. #1
    esallender is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default assignment help

    I have a 2 part assignment and i have hit a wall. The directions are aS FOLLOWS
    Part A
    Write an application to simulate the rolling of two standard six-sided dice. The application should use an object from the class Random to roll the first die and then again to roll the second. Each die can have the values of 1 to 6 and the sum of the dice can have values from 2 to 12. Use a one-dimensional array to tally the number of times each possible sum appears. The application should roll the dice 36,000 times. Display the results in a tabular format and determine if your results are reasonable.
    Part B
    Write an application that is an extension of part A by using a two-dimensional array to tally the actual combinations of rolls. A roll of 2 and 6 would be different than a roll of 6 and 2. The application should roll the dice 36,000 times. Display the results in a tabular format and determine if your results are reasonable.

    Part A I have heres the code.
    import java.util.Random;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.

    /**
    *
    * @author Ernest
    */
    public class RollDice_partA {

    public static void main(String[] args) {

    Random randomNumber = new Random();
    int[] frequency = new int[13];
    int die1;
    int die2;
    for (int roll = 1; roll <= 36000; roll++) {
    die1 = randomNumber.nextInt(6) + 1;
    die2 = randomNumber.nextInt(6) + 1;
    frequency[die1 + die2]++;
    }
    System.out.printf("%s%10s\n", " Sum", " Frequency");
    for (int sum = 2; sum <= 12; sum++) {
    System.out.printf("%4d%10d\n", sum, frequency[sum]);
    }
    }
    }

    Part B I can not figure out. I'm not even sure how to creata 2 d array. Help please ASAP.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you can do the first part, you should be able to do the second part, so don't give up. Declaring a 2-d array is easy. For instance if I wanted a 4 by 7 int array, I'd do:

    Java Code:
    int[][] my2dArray = new int[4][7];
    Why not play with your code and see what you can come up with. Again, I think that you've already done the hard part.

    Best of luck and welcome to the Java-Forms.org!

  3. #3
    esallender is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default progress

    ok made some progress. For sum reason it prints out the sam amout for every value every time. I don't think this is correct. any advise.

    Java Code:
    import java.util.Random;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
    
    /**
     *
     * @author Ernest
     */
    public class RollDice_partB {
    
        public static void main(String[] args) {
    
            Random randomNumber = new Random();
            int[][] frequency = new int[7][7];
            int die1 = 0;
            int die2 = 0;
            for (int roll = 1; roll <= 36000; roll++) {
                die1 = randomNumber.nextInt(6) + 1;
                die2 = randomNumber.nextInt(6) + 1;
                frequency[die1] [die2]++;
            }
            System.out.printf("%s%10s\n", "  Sum", "   Frequency");
            for (int sum = 2; sum <= 12; sum++) {
                System.out.printf("%4d%10d\n", sum, frequency[die1][die2]);
            }
        }
    }
    :confused:
    Last edited by Eranga; 10-25-2010 at 12:08 PM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to use two nested for loops and use the loop indices as your array indices to show all the different elements in the 2-D array.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Assignment
    By shinkymar in forum New To Java
    Replies: 2
    Last Post: 03-06-2010, 01:17 PM
  2. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 05:45 PM
  3. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 01:27 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •