Question: This code is incomplete, read the comments bellow. JAVA public class PiMonteCarloSkeleton{ /** * width of the square. */ private static final double WIDTH =

This code is incomplete, read the comments bellow. JAVA

public class PiMonteCarloSkeleton{

/** * width of the square. */ private static final double WIDTH = 1;

/** * Radius of the circle which is always half of squares {@link PiMonteCarlo#WIDTH}. */ private static final double RADIUS = WIDTH / 2;

/** * Convenience variable to be used in {@link PiMonteCarlo#containedInCircle()}. */ private static final double RADIUS_SQUARE = RADIUS * RADIUS;

/** * random generator. by placing a seed in {@link Random#Random(long)} * we can repeat the same numbers which can be very helpful for debugging. */ private final Random RAND;

/** * maximum number of points to be generated. */ private long threshold;

/** *

* create an {@link AtomicBoolean} as condition of keeping the thread alive, named alive * {@link AtomicBoolean} is thread safe, allowing multiple threads to change its value without race condition. * {@link AtomicBoolean} can be manipulated using {@link AtomicBoolean#get()}, {@link AtomicBoolean#set()} * and other similar methods. *

*/

/** *

* create two {@link LongAdder} as counters to keep track of dots in each square and circle. * {@link LongAdder} is thread safe, allowing multiple threads to change its value without race condition. * {@link LongAdder} can be manipulated using {@link LongAdder#longValue()}, {@link LongAdder#increment()} * and other similar methods. *

*/

/** *

* create a default constructor. you are to initialize {@link PiMonteCarlo#RAND}. *

*/

/** *

* complete {@link Runnable#run()} which is inherited from {@link Runnable}. * generate {@link PiMonteCarlo#threshold} points in a loop as long as {@link PiMonteCarlo#alive} is true * generate each point using {@link PiMonteCarlo#RAND} and {@link Random#nextDouble()}. * for each point generated call {@link LongAdder#increment()} for square counter and if * this point is true for {@link PiMonteCarlo#containedInCircle()}, * call {@link LongAdder#increment()} for circle counter as well. * finally print your result in following format, you can use System.out.printf(): * *

 * S:1000, C:781, C/S:0.7810000000, PI:3.1240000000, Real PI:3.1415926536 * 

* * in order: points in square, points in circle, {@link PiMonteCarlo#getRatio()}, * {@link PiMonteCarlo#getPI()}, real pi ({@link Math#PI}). *

*/

/** *

* return the ratio of dots in circle over overall dots (dots in square). * divide the value of 2 {@link LongAdder} class variables, circle over square. * remember to use {@link LongAdder#doubleValue()}, dividing suing long or int will remove the decimal precision. *

* @return ratio of dots in circle over square */ public double getRatio(){ return 0; }

/** *

* using the {@link PiMonteCarlo#getRatio()} estimate the value of pi. * value of Pi is estimated using: * *

 * pi_estimate ~= Area of Circle/Area of Square = (pi*r^2)/(2*r)^2 * => (pi*r^2)/(4*r^2) = pi/4 => pi ~= pi_estimate*4 * 

* * using pi ~= pi_estimate*4 and by getting pi_estimate from * {@link PiMonteCarloSkeleton#getRatio()} estimate the value of pi * @return PI */ public double getPI(){ return 0; }

/** *

* check to see if a point is with or on the circle. * we know a circle with center of (j, k) and radius (r)is represented as: * *

 * (x-j)^2 + (y-k)^2 = r^2 * 

* * we want to shift our circle from center 0,0 to * RADIUS,RADIUS so all of the circle is in positive range. * using the formula above as long as the left side is * smaller and equal to right side (x,y) is in the circle. *

* @param x - x coordinate of the point * @param y - y coordinate of the point * @return true of the point is in the circle */ private boolean containedInCircle( double x, double y){ return true; } /** *

* this method will start the thread which will start the point generation. * store the value of threshold in {@link PiMonteCarlo#threshold}. * use {@link PiMonteCarlo#setSeed(long)} to set new seed. * set {@link PiMonteCarlo#alive} to true to allow number generation condition to pass. * using {@link Thread} or {@link ExecutorService} class start the {@link PiMonteCarlo} thread. * {@link Thread} constructor takes a {@link Runnable} object and optional thread name as second argument. * upon calling {@link Thread#start()} on the Thread object the {@link Runnable#run()} method is called internally. *

* @param threshold - number of points to be generated * @param seed - to be used in {@link PiMonteCarlo#RAND} using {@link Random#setSeed()} */ public void simulate( long threshold, long seed) { } /** *

* chain to {@link PiMonteCarlo#simulate(long, long)} and use {@link System#currentTimeMillis()} * as seed for second argument of {@link PiMonteCarlo#simulate(long, long)}. *

* @param threshold - number of points to be generated */ public void simulate(long threshold) { }

/** *

* stop can be called to set {@link PiMonteCarlo#alive} to false. * forcing number generation to stop. *

*/ public void stop() { }

/** *

* set a new seed for {@link PiMonteCarlo#RAND} using {@link Random#setSeed()}. *

* @param seed - to be used in {@link PiMonteCarlo#RAND} using {@link Random#setSeed()} */ public void setSeed( long seed) { }

/** *

* in main instantiate {@link PiMonteCarlo} then call {@link PiMonteCarlo#simulate(long)} *

* @param args */ public static void main( String[] args){

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!