Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program that estimates the area of the dark gray shape. Your program should have two methods: a main(...) method and a second

Write a Java program that estimates the area of the dark gray shape. Your program should have two methods: a main(...) method and a second helper method that determines if a point is within a circle. The main(...) method should be composed of an outer while-statement that contains an if-statement. The while-statement should repeat several hundred-thousand times. The body of the while-statement should start by computing two random numbers in the range [0.0, 1.0). These are the landing coordinates of a thrown dart. If that randomly computed point falls within the larger circle, but not in either of the smaller circles, the dart has landed within the shape of interest. Keep track of two counts. The first count tracks the total number of iterations of the while-statement (i.e. the total number of times a dart lands on the square) and the second count tracks the number of times a dart lands in the shape. You will use your helper method to test if the random point is within the larger circle and not within either of the two smaller circles. After the while-statement completes, divide the second count by the first and print the result. This is the area estimate of the dark gray shape. 0.0 0.0 1.0 1.0 1.0 0.5 0.5 0.5 0.5 0.25 0.25 0.75 0.75 x y Your helper method will determine if a points random coordinates land within a circle. To perform this test, calculate the distance of the point from the center of the circle, and then test that distance against the radius of the circle. If the distance is less than the radius, the point is within the circle. Otherwise, it is outside the circle. Mathematically, a point with coordinates (x, y) is within a circle centered at (xc, yc) with radius r if the following is true. ()2 +()2 < Write a custom utility method with the following signature that determines if (x, y) is within a circle centered at (xc, yc) with radius r. Use this method to construct the if-statement within your while-statement to determine if your randomly generate point is within the larger circle but not in within of the two smaller circles. public static boolean inCircle(double x, double y, double xc, double yc, double r) Requirements

1. Create a new file called Lab04.java.

2. Add a main(...) method as the starting point for your program.

3. Add an inCircle(...) custom method following the instructions above. Consider using main(...) temporarily to test the inCircle(...) method to make sure it works properly before proceeding.

4. To main(...) add a while-statement that generates several hundred thousand random points.

5. Within the while-statement add an if-statement that uses inCircle(...) to determine if each randomly generated point falls within the larger circle and not within either of the smaller circles.

6. Count both the total number of while-statement iterations and the total number of times each point falls within the dark gray shape.

7. After the while-statement completes, compute and print the final ratio of the two counts.

8. Compile and test your program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

What are direct out-of-pocket costs?

Answered: 1 week ago