# Find pi with darts, Homework

Page 1 of 1

## 2 Replies - 1210 Views - Last Post: 15 January 2009 - 10:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=81040&amp;s=e28de06f8f490f848c297b3687d692b6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 YugoShavia

Reputation: 0
• Posts: 4
• Joined: 13-December 08

# Find pi with darts, Homework

Posted 15 January 2009 - 09:33 AM

Ok my program is supposed to use amount of times a dart is thrown * 4 * times hit to find pi. Ok did that. Put my teacher said that the math had to be set to another method. I hate methods anyway but how would you make a seperate method without having to state all of the variables as before. Could someone show me how to seperate the while statement to find pi into its own method please.

```import java.util.Scanner;
public class Darts
{

public static int computeHits (int timesThrown,double randomNumberx,double randomNumbery, double Throw, int counter, int Hits)
{
while(counter<=timesThrown)
{

randomNumberx =Math.random();
randomNumbery =Math.random();

randomNumberx = Math.pow(randomNumberx, 2);
randomNumbery = Math.pow(randomNumbery, 2);

Throw= randomNumberx + randomNumbery;

if(Throw<=1)
{
Hits++;
}
counter++;
}
return Hits;
}
public static void main(String[ ] args)
{
Scanner in = new Scanner(System.in);
int timesThrown;
int counter;
counter = 0;
double pi[]=new double [40];
double randomNumberx=0;
double randomNumbery=0;
int Hits= 0;
double Misses = 0;
int Throw=0;

int randomNumberCounter = 0;
randomNumberCounter++;
int trials = 0;
System.out.print("How many times do you want the dart to be thrown? ");
timesThrown=in.nextInt();

while(trials<=10)
{
double Hits2=computeHits (timesThrown,randomNumberx,randomNumbery,Throw,counter,Hits);
pi[trials]=4*Hits2/timesThrown;
System.out.println(pi[trials]);
trials++;

}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Find pi with darts, Homework

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Find pi with darts, Homework

Posted 15 January 2009 - 10:18 AM

I don't quite understand what he wants but what you could start by doing is to declare variables in their apropriate places.

```	int counter;
counter = 0;
double randomNumberx=0;
double randomNumbery=0;
int Hits= 0;
int Throw=0;

```

Those five are public in main and sent as parameters to your static computeHits-method. Just declare them inside the method and you don't need to have them as parameters. If you clean the code up a bit it will be simpler to modify later.

```	int randomNumberCounter = 0;
randomNumberCounter++;
double Misses = 0;

```

Those two are never used for anything.

```	int counter;
counter = 0;

```

Is same as writing

```	int counter = 0;

```

Post Edited

This post has been edited by Gloin: 15 January 2009 - 10:25 AM

### #3 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Find pi with darts, Homework

Posted 15 January 2009 - 10:27 AM

Oh, and now I realize that he actually wants you to move the while out from the method.