package mathComputations;

import java.lang.Math;
import java.io.*;

public class approxPi {
    // class provides a framework for checking whether the parentheses
    // in a line of input match

    public static void main (String args [])
        throws Exception {

        // declare needed objects
        InputStreamReader istream  = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(istream);
        PrintWriter out = new PrintWriter(System.out, true);

        double x, y; // coordinates for a randomly-generated point

        // Other declarations
        int N;         // Number of points to be selected
        String line;   // Line read
        int count = 0; // Count of points in circle
        int i;         // loop index
        boolean errorFound = false; // state of checking so far

        // read line from keyboard
        out.println ("Please enter the number of points to be selected");
        line = in.readLine();
        N = new Integer(line).intValue();

        for (i = 0; i < N; i = i + 1) {
            // pick a random x and y value
            x = Math.random(); 
            y = Math.random();
            // check if point is in circle of radius 1.0
            if (x*x + y*y <= 1.0)
                count = count + 1;
        }
        
        out.println ("In selecting " + N + " points in the unit square");
        out.println ("in the first quadrant, " + count + " points ");
        out.println ("were also in the unit circle.");
        out.println ("This gives " + 4.0*count/N 
                     + " as an approximation to Pi.");
    }
}

