0

I am having a little trouble with my program, I am supposed to estimate pi through a series of trials of throwing darts at a circular board of radius 1. Where I run into trouble is when I try to compile it comes up with the error "cannot find symbol - variable X". I don't really know why this is happening, I know that this error occurs when things are misspelled, but I know I did not misspell X. If you can help I would appreciate it very much!

/**
 * The purpose of this program is to calculate pi by throwing darts
 * 
 * 
 * 
 */
import java.util.Scanner;

public class Darts
{
   //create an array length number of darts for x and y
   //method to generate random numbers
   //calc pi = 4 hits / attempts
   //method to print

   public static int dartHit (double X, double Y, int hit)
   {
       X = Math.random();
       Y = Math.random();
       hit = 0;
       if ( Math.pow(X, 2) + Math.pow(Y, 2) <= 1)
       {
           hit++;
       }
       return hit;
   }

   public static double calcPi (int hit, int attempts, double pi)
   {
       pi = 4 * ((hit * 1.00) / (attempts * 1.00));
       return pi;
   }

   public static void main(String[ ] args)
   {
       Scanner in = new Scanner(System.in);

       System.out.println("How many darts per trial? ");
       int attempts = in.nextInt(); //(N)
       System.out.println("How many trials?");
       int trials = in.nextInt();

       double [] pies = new double [trials];
       double sum = 0.0;

       for (int j = 0; j < trials; j++)
       {
           for ( int i = 0; i < attempts; i++)
           {
               int dartHit = dartHit (X, Y, hit);
               double calcPi = calcPi(hit, attempts, pi);
               sum += calcPi;
           }
           double average = sum / attempts;
           pies [j] = average;
       }

       for (int k = 0; k < pies.length; k++)
       {
           System.out.printf( "Trial [" + k +"]: pi = " + "%8.6f \n", pies [k]);
       }
   }
}
S.Bean
  • 1
  • 2
  • `int dartHit = dartHit (X, Y, hit);` here both `X` and `Y` not available to `main()` both are local variables in `dartHit()` method. – SatyaTNV Feb 14 '16 at 15:34

1 Answers1

0

The problem is the main() method is not aware of the variables X, Y, hit, pi etc; which are defined in other methods. So you get a "cannot find symbol" for those lines. You may want to read up on scope of variables.

Here is a refactored version of your code:

import java.util.Scanner;

public class Darts
{
    //create an array length number of darts for x and y
    //method to generate random numbers
    //calc pi = 4 hits / attempts
    //method to print

    public static int dartHit (double X, double Y, int hit)
    {
        if ( Math.pow(X, 2) + Math.pow(Y, 2) <= 1)
        {
            hit++;
        }
        return hit;
    }

    public static double calcPi (int hit, int attempts, double pi)
    {
        pi = 4 * ((hit * 1.00) / (attempts * 1.00));
        return pi;
    }

    public static void main(String[ ] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.println("How many darts per trial? ");
        int attempts = in.nextInt(); //(N)
        System.out.println("How many trials?");
        int trials = in.nextInt();

        //Initialize variables
        double X = Math.random();
        double Y = Math.random();
        int hit = 0;

        double [] pies = new double [trials];
        double sum =0.0, pi = 0.0;

        for (int j = 0; j < trials; j++)
        {
            for ( int i = 0; i < attempts; i++)
            {
                int dartHit = dartHit (X, Y, hit);
                double calcPi = calcPi(dartHit, attempts, pi);
                sum += calcPi;
            }
            double average = sum / attempts;
            pies [j] = average;
        }

        for (int k = 0; k < pies.length; k++)
        {
            System.out.printf( "Trial [" + k +"]: pi = " + "%8.6f \n", pies [k]);
        }
    }
}
Bajal
  • 5,487
  • 3
  • 20
  • 25