Need compileable code in any language tell me which code you
Need compile-able code in any language, tell me which code you use. Will rate and thumb up, thanks!
The Monte-Carlo method tries to estimate a numerical quantity by using random sampling. We will try to use this method to approximately find certain areas. Imagine a disk of radius r with center at (a, b) where 0 lessthanorequalto a, b lessthanorequalto 1. Consider the intersection of this disk with the unit square [0, 1| times [0, 1]. We want to find the area of this intersection. The Monte carlo approach would do the following: Generate many random points inside the unit square. Find which of these also lie within the disk. The fraction of points in the disk is the area of the intersection (approximately) since the area of the unit square is 1. Write a program to do this. Your program should take as input three floating point numbers a, b, r and its output will be the approximate area of the intersection of the unit square with the disk (x - a)^2 + (y - b)^2 lessthanorequalto r^2. Experiment with how many random points you should choose to make the area estimate quite accurate. We will assume that r greaterthanorequalto 0.5 - this will guarantee that the intersection area is not too small.Solution
I\'ve used java to get this done.
PROGRAM CODE:
package intersection;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
//bean class to hold the x coordinate, y coordinate and the intersection value with circle center(a, b)
class Point
{
double x;
double y;
double intersection;
}
public class Intersection {
//function to calculate the intersection points
// Generate random points 20 times and then check if it is less than sqrt(r)
// Every point that is less than the sqrt(r) is stored separately and then the largest of it is desiplayed
public static void calculateIntersection(double a, double b, double r)
{
ArrayList<Point> points = new ArrayList<>();
double x = 0.00,y=0.00;
for(int i=0; i<20; i++)
{
x = generateRandom();
y = generateRandom();
double num = Math.pow((x-a), 2) + Math.pow((y-b), 2);
num = RoundOff(num);
if(num<=Math.pow(r, 2))
{
Point point = new Point();
point.x = x;
point.y = y;
point.intersection = num;
points.add(point);
}
}
if(points.size()>0)
{
Point largestPoint = points.get(0);
for(int k=1; k<points.size(); k++)
{
if(points.get(k).intersection>largestPoint.intersection)
{
largestPoint = points.get(k);
}
}
System.out.println(\"x: \" + largestPoint.x + \" y: \" + largestPoint.y);
}
}
//function to generate the random number
public static double generateRandom()
{
Random generator = new Random();
double ran = generator.nextDouble()*(1-0.5) + 0.5;
return RoundOff(ran);
}
// function to round off double to one decimal place
public static double RoundOff(double value)
{
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(1, RoundingMode.HALF_UP);
return bd.doubleValue();
}
//Main function to get the inputs - a,b,r and then call the method for calculation.
public static void main(String[] args) {
double a,b,r;
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the center of the circle: \");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println(\"Enter the radius: \");
r = sc.nextDouble();
sc.close();
calculateIntersection(a, b, r);
}
}
OUTPUT:
Enter the center of the circle:
0 0
Enter the radius:
1
x: 0.6 y: 0.8


