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

Java Programming You are to compute the value of PI by throwing darts at a targe

ID: 655158 • Letter: J

Question

Java Programming

You are to compute the value of PI by throwing darts at a target. The target is a circle embedded in a square. The circle has radius 1 and origin at (0,0), so the square will be two by two.

The area of a circle is: A = ?r2

Here we are using a unit circle, so r =1, so area = ?. The area of the square is 2x2=4, since the square is two units on a side. PI (or ?) can be roughly computed using the following ratio:

PI/4 = (circle hits)/(darts thrown) or PI = 4*(circle hits)/(darts thrown)

Your program is to compute the value of PI by throwing 10, 100, 1000, 10,000, 100,000, and 1,000,000 darts. It is to print out the value of PI for each of these and compare that to the value in math.PI and print out the difference. The computed value should be better the more darts are thrown.

Make certain to have comments at the top of your program with your name and problem 7 for OO Programming at the top.

Explanation / Answer


import java.util.Random;
public class valueOfPI {
private int x;
private int y;
public static void main(String[] args)
{
int START = -1;
int END = 1;
int circlehits=0;
int dartsthrown=10;
double PI;
Random random = new Random();
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is" + PI);
System.out.println("the value of difference is"+ Math.PI - PI);

dartsthrown=100;
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
double PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
System.out.println("the value of difference is"+ Math.PI - PI);
dartsthrown=1000;
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
System.out.println("the value of difference is"+ Math.PI - PI);
dartsthrown=10000;
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
System.out.println("the value of difference is"+ Math.PI - PI);
dartsthrown=100000;
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
System.out.println("the value of difference is"+ Math.PI - PI);
dartsthrown=1000000;
for (int idx = 1; idx <= dartsthrown; ++idx)
{
showRandomInteger(START, END, random);
if(Math.sqrt((double)(x*x+y*y))<=1.0)
circlehits+=1;
}
PI = 4*((double)circlehits/dartsthrown);
System.out.println("the value of PI is"+ PI);
System.out.println("the value of difference is"+ Math.PI - PI);
}


private static void showRandomInteger(int aStart, int aEnd, Random aRandom)
{
if (aStart > aEnd)
{
throw new IllegalArgumentException("Start cannot exceed End.");
}

long range = (long)aEnd - (long)aStart + 1;
long fraction = (long)(range * aRandom.nextDouble());
x = (int)(fraction + aStart);   
y= (int)(fraction + aStart);   
}
}

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