π / 4 = 1 - (1/3) + (1/5) - (1/7) + (1/9) - (1/11) + ...

There is another novel approach to calculate π. Imagine that you have a dart board that is 2 units square. It inscribes a circle of unit radius. The center of the circle coincides with the center of the square. Now imagine that you throw darts at that dart board randomly. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. The area of a circle with unit radius is just π square unit. The area of the dart board is 4 square units. The ratio of the area of the circle to the area of the square is π / 4.

To simuluate the throwing of darts we will use a random number generator. The Math class has a random() method that can be used. This method returns random numbers between 0.0 (inclusive) to 1.0 (exclusive). There is an even better random number generator that is provided the Random class. We will first create a Random object called randomGen. This random number generator needs a seed to get started. We will read the time from the System clock and use that as our seed.

Random randomGen = new Random ( System.currentTimeMillis() );

Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.

A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. There is a method nextDouble() that will return a double between 0.0 (inclusive) and 1.0 (exclusive). But we need random numbers between -1.0 and +1.0. The way we achieve that is:

double xPos = (randomGen.nextDouble()) * 2 - 1.0; double yPos = (randomGen.nextDouble()) * 2 - 1.0;

To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is Math.sqrt ( xPos * xPos + yPos * yPos ). The radius of the circle is 1 unit.

The class that you will be writing will be called CalculatePI. It will have the following structure:

import java.util.*; public class CalculatePI { public static boolean isInside ( double xPos, double yPos ) { ... } public static double computePI ( int numThrows ) { ... } public static void main ( String[] args ) { ... } }

In your method main() you want to experiment and see if the accuracy of
PI increases with the number of throws on the dartboard. You will compare
your result with the value given by Math.PI. The quantity *Difference*
in the output is your calculated value of PI minus Math.PI. Use the following
number of throws to run your experiment - 100, 1000, 10,000, and 100,000. You
will call the method computePI() with these numbers as input parameters. Your
output will be of the following form:

Computation of PI using Random Numbers Number of throws = 100, Computed PI = ..., Difference = ... Number of throws = 1000, Computed PI = ..., Difference = ... Number of throws = 10000, Computed PI = ..., Difference = ... Number of throws = 100000, Computed PI = ..., Difference = ... * Difference = Computed PI - Math.PI

In the method computePI() you will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will call the method isInside() to determine if the point is inside the circle or not. This you will do as many times as specified by the number of throws. You will keep a count of the number of times a dart landed inside the circle. That figure divided by the total number of throws is the ratio π / 4. The method computePI() will return the computed value of PI.

We will be looking at good documentation, descriptive variable names, and adherence to the coding convention mentioned below. Your file CalculatePI.java will have the following header:

/* File: CalculatePI.java Description: Student Name: Student UT EID: Course Name: CS 312 Unique Number: Date Created: Date Last Modified: */

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this: if ( x > 5 ) { a = b + c; } Not this: if ( x > 5 ) { a = b + c; }

Use the turnin
program to submit your **.java** file.
We should receive your work by 11 PM on Friday, 08 Mar 2013.
There will be substantial penalties if you do not adhere to the
guidelines.

- You must submit the .java file and not the .class file.
- Your .java file should have the header with the proper documentation.
- You should be submitting your .java file through the web based
*turnin*program. We will not accept files e-mailed to us. - Compile and run your code on the command line.
- Your code must compile before submission.
- Here is the Grading Criteria.