1. Member
Join Date
Jan 2009
Posts
26
Rep Power
0

## 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?");

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. ## 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

3. 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. i coudn't understand what you described here.

5. Member
Join Date
Dec 2008
Location
Italy
Posts
79
Rep Power
0
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. ## 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

#### Posting Permissions

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