Question
A unit circle with radius 1 should have an area of PI (PI * 1 squared). If the center of the circle is 0,0, then
A unit circle with radius 1 should have an area of PI (PI * 1 squared). If the center of the circle is 0,0, then the area of the top right quarter of the circle is PI/4. The top right quarter of the unit circle fits in a square with its lower left corner at 0,0 and its top right corner at 1,1, so any random point x,y where 0<=x<=1 and 0<=y<=1 will be in that top right quarter of the square. So if we generate n random points in that square, then PI/4 of them should be inside the circle. You can tell that a point x,y is inside the circle if it satisfies x squared + y squared <= 1.
Write a Java program to input how many points n to generate, generate n random points in the square that encloses the top right corner of the unit circle, and count how many of those points are also in the unit circle, m. Then, output m/n*4, which should be an approximation of PI. Also, ouput Math.PI, which is pre-defined, and the difference between your approximation and Math.PI.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started