Results 1 to 6 of 6
- 01-03-2009, 07:20 PM #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?");
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.
- 01-03-2009, 07:52 PM #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:
CJSL...write a main program that will call your function...Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-04-2009, 01:42 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Starr29, please use code tags when you are posting again. It's really hard to read un-formatted code sinpest here in the forum.
- 01-06-2009, 09:59 AM #4
i coudn't understand what you described here.
- 01-06-2009, 01:29 PM #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
This construct works for integers, chars and Enum typesJava Code:switch (diceTotal) { case 2: returnValue2++; totalRolls++; break; ... case 9: returnValue9++; totalRolls++; break; .... case 12: returnValue12++; totalRolls++; break; }
- 01-06-2009, 03:38 PM #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:
OK, so now you know that you have to write a method that performs this activity 10,000 times for each total of dice.Write a function that performs the experiment of rolling to get a given total 10000 times
Now you know that the method's parameter is going to be the dice total (2-12).Each individual experiment should be done by calling the function ...
The desired total is a parameter to the subroutine.
Finally, the main method is going to do the totaling of the results and display it.write a main program that will call your function once for each of the possible totals (2, 3, ..., 12).
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
a problem about java mail client program
By lunarstyle in forum AWT / SwingReplies: 9Last Post: 12-19-2009, 04:14 PM -
[SOLVED] Ready to Program Java IDE
By Starr29 in forum New To JavaReplies: 9Last Post: 07-24-2009, 05:21 PM -
[SOLVED] Ready to Program Java IDE Problem #3 PacMan For Loop to Slide Across the Scr
By Starr29 in forum New To JavaReplies: 3Last Post: 07-18-2009, 01:26 PM -
[SOLVED] Ready to Program Java IDE Problem #2
By Starr29 in forum New To JavaReplies: 2Last Post: 01-03-2009, 01:02 AM -
getting problem in compiling java program?
By sathish04021984 in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks