Thread: Need help with doing a calculation in Java

1. Member
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);

}

}
}```

2. Member
Join Date
Feb 2009
Posts
7
Rep Power
0
Might this work: piTotals = piTotals + pi;

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
Originally Posted by John D.
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;
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.

4. Member
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

5. Moderator
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.

6. Member
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);

}
}```

7. Moderator
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();
}```
It's up to you at what point you factor out code and make a new method - again, personally, I would think twice about the one line methods. But name them according to their function eg isHit() rather than numberHit().

(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
}```
with

Java Code:
```if(getFoo()) {
// something
} else {
// something else
}```
And in fact you might want to think about whether that else part and its associated variable are even needed!

But the major thing is that your code is giving a sensible estimate for pi.

Posting Permissions

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