Results 1 to 7 of 7
 02242009, 09:52 PM #1Member
 Join Date
 Feb 2009
 Posts
 7
 Rep Power
 0
Need help with doing a calculation in Java
In this program, I am calculating the value of pi by simulating throwing darts at a dartboard. I have correctly coded the formula that calculates the values of pi, however, as seen in the code, I can't figure out how to calculate the average of the pi values that I have calculated. I know I need to get the sum of all the values of pi, and divide that by the number of trials; just not sure how to implement that in code into the program. Can somebody help with this? Thanks
Java Code:/** * The purpose of this program is to calculate the value of pi by simulating throwing darts at a dart board. * * @author John D. Barry * @date 02/24/09 */ import java.util.Scanner; public class Darts { //main method public static void main (String [ ] args) { Scanner in = new Scanner(System.in); System.out.println("How many times should the darts be thrown in a trial? "); int drops = in.nextInt(); System.out.println(); System.out.println("Enter the number of trials. "); double numberOfTrials = in.nextDouble(); System.out.println(); for (int trialNumbers = 0; trialNumbers < numberOfTrials; trialNumbers++) { double numberOfX = 0; double numberOfY = 0; double numberOfHit = 0; double numberOfMiss = 0; double pi = 0; double average = 0; while (numberOfX < drops && numberOfY < drops) { numberOfX++; numberOfY++; double xValue = Math.random() * 2 + 1; double yValue = Math.random() * 2 + 1; if ((Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) <= 1) { numberOfHit++; } else if ((Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) > 1) { numberOfMiss++; } } pi = 4 * (numberOfHit / drops); System.out.printf("Trial " + trialNumbers + ": pi = %12f\n", pi); } } }
 02242009, 10:14 PM #2Member
 Join Date
 Feb 2009
 Posts
 7
 Rep Power
 0
Might this work: piTotals = piTotals + pi;
 02242009, 10:16 PM #3Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
It looks like you need another variable to hold the accumlating estimate of pi.
* declare it before the trialNumbers for loop and initialise to zero.
* at the end of the loop add the value of pi that was observed
* after the for loop has finished divide it by numberOfTrials
(What you are really doing is counting the grand total number of hits over all the trials and dividing by the number of darts that were thrown which is numberOfTrials*numberOfDrops.)
It might be a good idea to use separate methods. Eg you could have a method to prompt and return an integer response. (any reason why the number of trials is a double?) And another to return the number of hits observed in a single trial.
 02242009, 10:24 PM #4Member
 Join Date
 Feb 2009
 Posts
 7
 Rep Power
 0
Ok. Thanks for the help. I see what you are saying. I am trying to implement it into my program. Thanks
 02242009, 10:56 PM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
I didn't see your previous post  but I think we're saying the same thing.
Post back with your code if you have problems.
 02242009, 11:25 PM #6Member
 Join Date
 Feb 2009
 Posts
 7
 Rep Power
 0
How does this look for using separate methods?
Java Code:/** * The purpose of this program is to calculate the value of pi by simulating throwing darts at a dart board. * * @author John D. Barry * @date 02/24/09 */ import java.util.Scanner; public class Darts { public static int dropsUserInput() { Scanner in = new Scanner(System.in); System.out.println("How many times should the darts be thrown in a trial? "); int drops = in.nextInt(); return drops; } public static int trialsUserInput() { Scanner in = new Scanner(System.in); System.out.println("Enter the number of trials. "); int numberOfTrials = in.nextInt(); return numberOfTrials; } public static double theXValue() { return Math.random() * 2 + 1; } public static double theYValue() { return Math.random() * 2 + 1; } public static boolean numberHit(double xValue, double yValue) { return (Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) <= 1; } public static boolean numberMiss(double xValue, double yValue) { return (Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) > 1; } public static void printResultsOne(int trialNumbers, double pi) { System.out.printf("Trial " + trialNumbers + ": pi = %12f\n", pi); } public static void printResultsTwo(double average) { System.out.printf("Estimate of pi = %9f\n ", average); } //main method public static void main (String [ ] args) { double piTotals = 0; int firstUsersInput = dropsUserInput(); int secondUsersInput = trialsUserInput(); for (int trialNumbers = 0; trialNumbers < secondUsersInput; trialNumbers++) { double numberOfX = 0; double numberOfY = 0; double numberOfHit = 0; double numberOfMiss = 0; double pi = 0; double average = 0; while (numberOfX < firstUsersInput && numberOfY < firstUsersInput) { numberOfX++; numberOfY++; double xValue = theXValue(); double yValue = theYValue(); boolean numberOfHits = numberHit(xValue,yValue); boolean numberOfMisses = numberMiss(xValue,yValue); if (numberOfHits) { numberOfHit++; } else if (numberOfMisses) { numberOfMiss++; } } pi = 4 * (numberOfHit / firstUsersInput); piTotals = piTotals + pi; printResultsOne(trialNumbers,pi); } double average = piTotals / secondUsersInput; printResultsTwo(average); } }
 02252009, 12:44 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
Looks good!
Personally I'd just have one input method that was used for both get both ints:
Java Code:static int getIntResponse(String prompt) { Scanner in = new Scanner(System.in); System.out.println(prompt + " "); return in.nextInt(); }
(I have just realised you might have chosen that name because I had suggested a method to count the hits in a single trial. Your method is doing something slightly different: returning the result for a single dart within a single trial.)
Notice that there is a bit of redundancy in the numberHit()/numberMiss() methods. It's going to be one or the other. The code will look a bit cleaner if you replace something like this:
Java Code:boolean foo = getFoo(); boolean notFoo = getNotFoo(); if(foo) { // something } else if(notFoo) { // something else }
Java Code:if(getFoo()) { // something } else { // something else }
But the major thing is that your code is giving a sensible estimate for pi.
Similar Threads

Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07262008, 04:17 PM 
RSSI calculation using Java Card STK....
By vickytulla in forum Advanced JavaReplies: 0Last Post: 07142008, 08:56 AM 
Area Calculation: Add, Substract, XOR
By Java Tip in forum java.awtReplies: 0Last Post: 06222008, 11:08 PM 
Two problem for my rssi calculation program
By iamchoilan in forum Advanced JavaReplies: 3Last Post: 04252008, 03:49 PM 
Problem with Calculation ....
By danny000 in forum New To JavaReplies: 1Last Post: 04152008, 02:42 PM
Bookmarks