Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise you will write a program that estimates the value of Pi. Preten

ID: 3564963 • Letter: I

Question

In this exercise you will write a program that estimates the value of Pi. Pretend that we are looking at a dartboard. If the square is 2units wide and 2 units high, the radius of the circle within the square is 1 unit. Then the area of the circle is PI*r2 = PI*12 = PI. The area of the square will be 2*2 = 4. The estimation of PI will work by randomly throwing dartsat the dartboard. We will assume that every dart will hit the board but the location of that strike will be random so some darts will land inside the circle and some will land outside the circle. The calculation of PI is then the ratio of the number of darts that land inside the circle to the total number of darts, multiplied by 4.

Note that as we increase the number of darts used in the simulation, the accuracy improves. Follow the steps to complete the program:

1. Declare variables that represent x, y coordinates of a dart, the distance of the dart from the origin, the number of darts that land inside the circle, and the total number of darts.

2. Read in the total number of darts (N) from the user and create an instance of the Random class.

3. Create a loop that runs N times. Inside the loop, you will use the nextFloat() method of the Random class to create x, y coordinates randomly, and then compute the distance of the dart from the origin. Determine whether the dart lands inside or outside of the circle.

4. Compute PI using the estimation algorithm.

5. Run the program with increasing numbers from 100 to 100,000,000 and observe the accuracy of PI.

Explanation / Answer

The test class first

public class TestCalculatePi {

   public static void main(String[] args) {
      
       int noOfThrows = 100;
      
       PICalculator piCal1 = new PICalculator(noOfThrows);

       System.out.println("The calculated value of pi for "
                           + noOfThrows +" throws is: " + piCal1.calculatePi());

       int bigNoOfThrows = 100000000;
       PICalculator piCal2 = new PICalculator(bigNoOfThrows);
      
       System.out.println("The calculated value of pi for "
               + bigNoOfThrows +" throws is: " + piCal2.calculatePi());

      
   }
}

public class CoordinatePoint {
  
   public static float origin_x = 0;
   public static float origin_y = 0;
  
   private float x;
   private float y;

   public float getX() {
       return x;
   }

   public void setX(float x) {
       this.x = x;
   }

   public float getY() {
       return y;
   }

   public void setY(float y) {
       this.y = y;
   }
  
   public double getDistanceFromOrigin() {
       return (Math.sqrt(
                   Math.pow(x, 2) + Math.pow(y, 2)
                   ));
   }

}

import java.math.BigDecimal;
import java.util.Random;

public class PICalculator {
  
   public static Random random = new Random();
   public double N;
   public double dartsInside = 0;
   public static int squareSide = 2;
   public static int radius = squareSide/2;

   public PICalculator(int n) {
       N = n;
   }

   public double calculatePi() {
       CoordinatePoint randPoint = new CoordinatePoint();
       for (int i = 0; i < N; i++) {
           randPoint.setX(randDouble(CoordinatePoint.origin_x, squareSide/2));
           randPoint.setY(randDouble(CoordinatePoint.origin_y, squareSide/2));

           double distFromOrigin = randPoint.getDistanceFromOrigin();
           if(distFromOrigin <= radius)
               dartsInside++;
       }
       BigDecimal ratio = new BigDecimal(dartsInside/N);
       BigDecimal area = new BigDecimal(squareSide*2);
       BigDecimal pi = ratio.multiply(area);
       return pi.doubleValue();
   }
  
   public static float randDouble(float min, float max) {
       return random.nextFloat() * (max - min) + min;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote