Program the following simulation: Darts are thrown at random points onto the square with corners (1,1) and (−1,−1). If the dart lands inside the unit circle (that is, the circle with center (0,0) and radius 1), it is a hit. Otherwise it is a miss. Run this simulation and use it to determine an approximate value for π. Extra credit if you explain why this is a better method for estimating π than the Buffon needle program.

Complete the following files:

Dart.java

public class Dart { // private implementation . . . public Dart() { . . . } /** Throws a dart into the square [-1,1] x [1,1] and records whether it hits the unit circle. */ public void throwIntoSquare() { . . . } /** Get the number of hits inside the unit circle. @return hits number of hits */ public int getHits() { . . . } /** Get the number of tries. @return the number of times the dart was thrown. */ public int getTries() { . . . } }

DartSimulator.java

Use the following file:

DartTester.java

public class DartTester
{
   public static void main(String[] args)
   {
      Dart d = new Dart();
      d.throwIntoSquare();
      System.out.println(d.getTries());
      System.out.println("Expected: 1");
      int h = d.getHits();
      System.out.println(h == 0 || h == 1);
      System.out.println("Expected: true");      
   }
}