2 Replies - 1064 Views - Last Post: 15 January 2009 - 10:27 AM Rate Topic: -----

#1 YugoShavia  Icon User is offline

  • New D.I.C Head

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

Find pi with darts, Homework

Post icon  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  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1