Results 1 to 8 of 8
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Did I use arrays right in my Java code?

    My program is supposed to roll to determine how many time the number 2comes up when an 11 sided (numbers 2 to 12) and two six sided dice (numbers 1 to 6 on each die. the outcome of each die is added together) is rolled 1000 times. The program should output how many times the 11-sided and the 6-sided dice resulted in the number two after 1000 rolls.

    Can someone tell me if I carried out the arrays properly throughout my code? thanks


    CODE:
    import java.io.*;

    public class arrayAssignment
    {

    public static void main (String [] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed

    int rollSix [] = new int [1000];
    int rollEleven [] = new int [1000];

    System.out.println("Two six sided dice: ");
    System.out.println("After 1000 rolls, the number 2 came up " + rollDiceA (rollSix) + " times.");
    System.out.println("~~~~~~~~~~~~~~~~~~~~…
    System.out.println("Eleven sided die: ");
    System.out.println("After 1000 rolls, the number 2 came up " + rollDiceB (rollEleven) + " times.");

    }

    public static int rollDiceA (int rollDiceA []) throws IOException
    {
    int rollNum = 0;
    for (int i = 0; i < 1000; i++)
    {
    int resultA = (int)(Math.random()*6) + 1;
    int resultB = (int)(Math.random()*6) + 1;
    int sum = resultA + resultB;

    if (sum == 2)
    {
    rollNum++;
    }
    }
    return rollNum;
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Did I use arrays right in my Java code?

    First, you should put your formatted code between code tags .

    Now:


    1. The array that you pass to the rollDiceA method is not initialized to anything.
    2. And you never reference the array iinternal to the method.
    3. You didn't include the rollDiceB method



    Having said that, I don't believe you need arrays. You're just summing the times when you roll a two and then returning the number. Arrays don't appear to play any part in that.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Did I use arrays right in my Java code?

    thanks for the help!
    oops i forgot to paste the third method. this is it:
    import java.io.*;

    public class arrayAssignment
    {

    public static void main (String [] args)
    {

    int rollSix [] = new int [1000];
    int rollEleven [] = new int [1000];

    System.out.println("Two six sided dice: ");
    System.out.println("After 1000 rolls, the number 2 came up " + rollDiceA (rollSix) + " times.");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~");
    System.out.println("Eleven sided die: ");
    System.out.println("After 1000 rolls, the number 2 came up " + rollDiceB (rollEleven) + " times.");

    }

    public static int rollDiceA (int rollDiceA [])
    {
    int rollNum = 0;
    for (int i = 0; i < 1000; i++)
    {
    int resultA = (int)(Math.random()*6) + 1;
    int resultB = (int)(Math.random()*6) + 1;
    int sum = resultA + resultB;

    if (sum == 2)
    {
    rollNum++;
    }
    }
    return rollNum;
    }
    public static int rollDiceB (int rollDiceB [])
    {
    int rollNum = 0;
    for (int i = 0; i < 1000; i++)
    {
    int result = (int)(Math.random()*12) + 2;

    if (result == 2)
    {
    rollNum++;
    }
    }
    return rollNum;
    }
    }



    The assignment the teacher gave to us is asking for us to use arrays, is it possible to incorporate them into my program? Thanks :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Did I use arrays right in my Java code?

    Well, you don't need to pass in any arrays. Just record each toss into the array and then return the arrays. Then you could then iterate thru them and count the twos.

    Example:

    Java Code:
    int [] twoDice = new int[1000];
    twoDice[] = rollTwoDice();
    
    // Tally the results
    
    
    
    public int[] rollTwoDice() {
    int[]  toss = new int[1000];
    // processing
    
    return toss;
    }
    There are certainly better ways to do it, even with arrays. But this should be a reasonable start.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Did I use arrays right in my Java code?

    Thanks Jim!
    Just to clarify, would this mean I would remove the arrays I wrote at the beginning of my program and would this replace my (Math.random) lines as well as the counter? Sorry for all the questions, I am very new to Java. Thanks.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Did I use arrays right in my Java code?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: Did I use arrays right in my Java code?

    Before you start coding you should apply a bit of (simple) math: for the 11 sided die the probability of side 2 is 1/11 (unless it has been fiddled with) but for the two 6 sided dice the probability for two 1s is 1/6*1/6 == 1/36; so for 1000 rolls the probabilities/totals are 1000/11 and 1000/36; and b.t.w. you don't need arrays.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Did I use arrays right in my Java code?

    Quote Originally Posted by JosAH View Post
    ... and b.t.w. you don't need arrays.

    kind regards,

    Jos
    Yeah, that's what I said. But the prof supposedly want's his students to do so.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Question about some lines of code arrays
    By silverglade in forum New To Java
    Replies: 9
    Last Post: 06-09-2011, 04:59 AM
  2. Fill in the blank code for arrays
    By chuckypalumbo in forum New To Java
    Replies: 1
    Last Post: 05-02-2011, 01:17 AM
  3. 2d Arrays Code help!
    By Meta in forum New To Java
    Replies: 12
    Last Post: 04-04-2010, 06:49 AM
  4. Replies: 2
    Last Post: 11-18-2008, 10:44 PM
  5. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:11 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
  •