In this program you estimate the famous mathematical constant π by...throwing darts at a dart board. Actually you simulate throwing darts because we are programming computers, and computers are ideal for running simulations. The theory is this: start with a square that measures 2 on each side; its area is 4. Inscribe a circle inside the square with a radius of 1; its area is πr2 or π. Randomly throw a bunch of 'darts' at the square dart board and count how many of the darts land inside the circle. The ratio darts inside circle :: total darts should be close to π :: 4. So π can be estimated as equal to 4 * (darts inside circle / total darts). (Caution: don't try this with an actual dart board. You are likely to lose friends.)
We'll use the objectdraw library to create an Applet. Information on objectdraw can be found under Help / objectdraw on the class Web page.
private instance variables
This method creates the button that, when clicked, throws 1 dart. First create the button with a call to 'FilledRoundedRect'. An example:
add1 = new FilledRoundedRect(50, 50, 200, 100, 3, 3, canvas);
Then add Text to the button, for example:
Text T1 = new Text("Add 1 Point",60,60,canvas);
Then (optionally) set the text's color and font as you wish. An example:
Font myFont = new Font("SansSerif",Font.BOLD, 18);
Similar to add1Button(), this method causes 100 darts to be thrown.
Causes 10,000 darts to be thrown. Duck!
Create a 'new FilledRect()' for the dart board. I recommend starting coordinates of (100, 0), and a height and width of 400. Create a 'new FilledOval()' for the circle. It should fit precisely inside the square dart board. You can use 'setColor()' to change the colors of both objects.
Create a random-number generator. We covered this briefly in class; here is the slide. Also create a Location object, and 2 integers to store an X and a Y coordinate. Set up a FOR loop that runs 'max' times. Inside the loop, create a random X coordinate that lies inside the dart board. Generating this coordinate so that it lies between 100 and 499 (inclusive) seemed to work well. Similarly, create a random Y coordinate that lies between 0 and 399 (inclusive). Assign the X and Y coordinates to the Location object. I could not find a 'Point' object within objectdraw, so I used a very short Line to represent a point. An example:
new Line(numX, numY, numX, numY, canvas);
draws the a point on the dart board. Increment the total number of darts thrown. If the circle contains the Location of the thrown dart, also increment the number of darts inside the circle.
After the FOR loop ends, call 'updatePIEstimate();'.
Calculate an estimate for π as 4.0 * number of darts inside circle / total number of darts. Call the method 'white()'. Use calls to 'Text' to display your updated estimate, the actual value of π, the difference between π and your estimate, and the number of darts thrown. An example of a call to 'Text':
new Text("Actual Value of PI: " + Math.PI,500,100, canvas);
This method is necessary to create a 'clean slate' for writing output. Make a call to 'new FilledRect' to create a rectangle to cover the area to which you write in 'updatePIEstimate()'.
This method runs when the user clicks the mouse. Set up an IF - ELSE IF - ELSE IF structure with the logic "if button 1 contains the Location of the mouse click, call 'addPoints(1)'. On the other hand, if button 2 contains the mouse click, call 'addPoints(100)'. Finally, if button 3 contains the mouse click, call 'addPoints(10000)'." The syntax would look like this:
'begin()' is like 'main(),' it runs when the Applet loads. In 'begin()' call 'drawBoard()' and the 3 methods that draw the buttons.
Here is how my Applet looks upon first starting up:
Here is how the Applet appears after 100 'darts' have been thrown:
You can download EstimatePI.java to use as a starting point in writing your program.