# Return true if the point is inside the circle false

• Homework Help
• 4
• 87% (15) 13 out of 15 people found this document helpful

This preview shows page 2 - 4 out of 4 pages.

* * @return true if the point is inside the circle, false otherwise */ private static boolean pointIsInCircle(double xCoord, double yCoord) { //To declare a temp value for the parameters. double xTemp = xCoord, yTemp = yCoord;
boolean circle = false; //To determine whether the point in in the circle. if ((xTemp - 1) * (xTemp - 1) + (yTemp - 1) * (yTemp - 1) <= 1.0) { circle = true; } return circle; } /** * Generates n pseudo-random points in the [0.0,2.0) x [0.0,2.0) square and * returns the number that fall in the circle of radius 1.0 centered at the * point (1.0, 1.0). * * @param n * the number of points to generate * @return the number of points that fall in the circle */ private static int numberOfPointsInCircle(int n) { // Declare the temp values and the random class. int points = n, num = 0; Random rnd = new Random1L(); //Use for loop to count the numbers of points in the circle for (int i = 0; i < points; i++) { double x = 2.0 * rnd.nextDouble(); double y = 2.0 * rnd.nextDouble(); if (pointIsInCircle(x, y)) { num++; } } return num; } 4. Rewrite the  main  method in your solution to the Monte Carlo estimation of    lab so that it uses  numberOfPointsInCircle  (and indirectly  pointIsInCircle π ). /** * Main method. * * @param args * the command line arguments; unused here */ public static void main(String[] args) { /* * Open input and output streams */ SimpleReader input = new SimpleReader1L(); SimpleWriter output = new SimpleWriter1L(); /* * Ask user for number of points to generate */ output.print("Number of points: "); int n = input.nextInteger(); /* * Declare counters and initialize them */ /* * Create pseudo-random number generator */
/* * Estimate percentage of points generated in [0.0,2.0) interval that * fall in the circle subinterval */ // I put the numberOfPointsInCircle() method in class Method in the same package and just call it by Method.numberOfPointsInCircle(). double estimate = (PERCENT * Method.numberOfPointsInCircle(n)) / n; double areaGuessPi = FOUR * estimate / PERCENT; output.println("Estimate of percentage: " + estimate + "%"); output.println("Area estimate of pi: " + areaGuessPi); /* * Close input and output streams */ input.close(); output.close(); } 5. }