Implement (i.e., provide the code for the body of) the static method declared as
ID: 3663299 • Letter: I
Question
Implement (i.e., provide the code for the body of) the static method declared as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Checks whether the given point (xCoord, yCoord) is inside the circle of
* radius 1.0 centered at the point (1.0, 1.0).
*
* @param xCoord
* the x coordinate of the point
* @param yCoord
* the y coordinate of the point
* @return true if the point is inside the circle, false otherwise
*/
private static boolean pointIsInCircle(double xCoord, double yCoord) {
...
}
Implement the static method declared as follows:
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 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) {
...
}
Note: Use pointIsInCircle in the implementation of numberOfPointsInCircle.
Rewrite the main method in your solution to the Monte Carlo estimation of lab so that it uses numberOfPointsInCircle (and indirectlypointIsInCircle).
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Checks whether the given point (xCoord, yCoord) is inside the circle of
* radius 1.0 centered at the point (1.0, 1.0).
*
* @param xCoord
* the x coordinate of the point
* @param yCoord
* the y coordinate of the point
* @return true if the point is inside the circle, false otherwise
*/
private static boolean pointIsInCircle(double xCoord, double yCoord) {
...
}
Explanation / Answer
alex krieger
import components.random.Random;
import components.random.Random1L;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
public class MonteCarlo3 {
public static void main(String[] args){
SimpleReader input = new SimpleReader1L();
SimpleWriter output = new SimpleWriter1L();
int n = 0;
int k = 0;
//int nThrows = 0;
//int nSuccess = 0;
double xCoord = 0;
double yCoord = 0;
int ptsInInterval = 0;
output.print("Number of points: ");
n = input.nextInteger();
for (i = 0; i < 1000000 ; i++)
{
x = Math.random(); // Throw a dart
y = Math.random();
nThrows++;
if ( x*x + y*y <= 1 )
nSuccess++;
}
System.out.println("Pi/4 = " + (double)nSuccess/(double)nThrows );
System.out.println("Pi = " + 4*(double)nSuccess/(double)nThrows );
}
/**
* Checks whether the given point (xCoord, yCoord) is inside the circle of
* radius 1.0 centered at the point (1.0, 1.0).
*
* @param xCoord
* the x coordinate of the point
* @param yCoord
* the y coordinate of the point
* @return true if the point is inside the circle, false otherwise
*/
private static boolean pointIsInCircle(double xCoord, double yCoord) {
boolean pointIsInCircle = true;
if(xCoord*xCoord + yCoord*yCoord <= 1){
return pointIsInCircle;
}
else{
pointIsInCircle = false;
return pointIsInCircle;
}
}
/**
* 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) {
int numberOfPointsInCircle = 0;
int ptsInInterval = 0;
while (ptsInInterval < n) {
boolean point = pointIsInCircle(xCoord, yCoord);
double x = (2*Math.random());
ptsInInterval++;
if (point==true) {
numberOfPointsInCircle++;
}
}
return numberOfPointsInCircle;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.