Results 1 to 6 of 6
  1. #1
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Ready to Program Java IDE Problem #4

    Hi,

    Can you please walk me through with a description how to write the code step by step for the following problem:?

    This exercise builds on Exercise 2. Every time you roll the dice repeatedly, trying to get a given total, the number of rolls it takes can be different. The question that naturally arises is what the average number of rolls is. Write a function that performs the experiment of rolling to get a given total 10000 times. The desired total is a parameter to the subroutine. The average number of rolls is the return value. Each individual experiment should be done by calling the function you wrote for exercise 2. Now, write a main program that will call your function once for each of the possible totals (2, 3, ..., 12). It should make a table of the results, something like:

    Total on Dice Average Number of Rolls
    -----------------------------------------------------------
    2 35.8382
    3 18.0607
    . .
    . .


    I wrote this:

    // The "Unit4Activity6Assignment3" class.
    import java.awt.*;
    import hsa.Console;

    public class Unit4Activity6Assignment3
    {
    static Console c; // The output console

    public static void main (String[] args)
    {
    c = new Console ();

    // Declare variables
    int die1, die2, number;
    int diceTotal = 0, totalRolls = 0; // Initialize variables
    double returnValue2 = 0, returnValue3 = 0, returnValue4 = 0, returnValue5 = 0, returnValue6 = 0;
    double returnValue7 = 0, returnValue8 = 0, returnValue9 = 0, returnValue10 = 0, returnValue11 = 0, returnValue12 = 0;

    c.println ("What total (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, or 12) do you want to roll for a");
    c.println ("given total of 10000 times?");
    number = c.readInt();

    while (number != 10000)
    {
    die1 = (int)(Math.random()*6) + 1;
    die2 = (int)(Math.random()*6) + 1;
    diceTotal = die1 + die2;

    if (diceTotal == 2)
    {
    returnValue2 = returnValue2 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 3)
    {
    returnValue3 = returnValue3 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 4)
    {
    returnValue4 = returnValue4 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 5)
    {
    returnValue5 = returnValue5 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 6)
    {
    returnValue6 = returnValue6 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 7)
    {
    returnValue7 = returnValue7 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 8)
    {
    returnValue8 = returnValue8 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 9)
    {
    returnValue9 = returnValue9 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 10)
    {
    returnValue10 = returnValue10 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 11)
    {
    returnValue11 = returnValue11 + 1;
    totalRolls = totalRolls + 1;
    }

    else if (diceTotal == 12)
    {
    returnValue12 = returnValue12 + 1;
    totalRolls = totalRolls + 1;
    }



    }
    c.println ("Total on Dice Average Number of Rolls");
    c.println ("-----------------------------------------");
    c.println (totalRolls / returnValue2);
    c.println (totalRolls / returnValue3);
    c.println (totalRolls / returnValue4);
    c.println (totalRolls / returnValue5);
    c.println (totalRolls / returnValue6);
    c.println (totalRolls / returnValue7);
    c.println (totalRolls / returnValue8);
    c.println (totalRolls / returnValue9);
    c.println (totalRolls / returnValue10);
    c.println (totalRolls / returnValue11);
    c.println (totalRolls / returnValue12);




    } // main method
    } // Unit4Activity6Assignment3 class

    As you can see, my code is incorrect.

    Thanks.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Function vs method

    To begin with, the description of the assignment should refer to a "method" not a "function" (not your fault) as this is Java, but does make one think how well the problem is planted....
    Anyway... so where is your method (aka function)? The assignment states:
    ...write a main program that will call your function...
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    Starr29, please use code tags when you are posting again. It's really hard to read un-formatted code sinpest here in the forum.

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    i coudn't understand what you described here.

  5. #5
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    It's really hard to understand your error, please post your code inside CODE tags (the # button you see)

    Anyway I have a suggestion to you. It's really boring to read/write lots of if-clauses... Do you know the existence of switch control statement?
    Your code should look like this
    Java Code:
    switch (diceTotal) {
       case 2: returnValue2++; totalRolls++; break;
       ...
       case 9: returnValue9++; totalRolls++; break;
       ....
       case 12: returnValue12++; totalRolls++; break;
    }
    This construct works for integers, chars and Enum types

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default What a messy description

    This has to be one of the worst assigment descriptions I have ever tried to read. Anyway...
    What the assignement is trying to do is find out how many times does it takes to get a certain total of dice 10,000 times (like how many times do you have to throw the dice to get a total of 5 10,000 times).
    Some important observations:
    Write a function that performs the experiment of rolling to get a given total 10000 times
    OK, so now you know that you have to write a method that performs this activity 10,000 times for each total of dice.
    Each individual experiment should be done by calling the function ...
    The desired total is a parameter to the subroutine.
    Now you know that the method's parameter is going to be the dice total (2-12).
    write a main program that will call your function once for each of the possible totals (2, 3, ..., 12).
    Finally, the main method is going to do the totaling of the results and display it.

    Do you know how to implement a method? (because I don't see one in your code). If not, read these:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. a problem about java mail client program
    By lunarstyle in forum AWT / Swing
    Replies: 9
    Last Post: 12-19-2009, 05:14 PM
  2. [SOLVED] Ready to Program Java IDE
    By Starr29 in forum New To Java
    Replies: 9
    Last Post: 07-24-2009, 06:21 PM
  3. Replies: 3
    Last Post: 07-18-2009, 02:26 PM
  4. [SOLVED] Ready to Program Java IDE Problem #2
    By Starr29 in forum New To Java
    Replies: 2
    Last Post: 01-03-2009, 02:02 AM
  5. getting problem in compiling java program?
    By sathish04021984 in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:26 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
  •