import java.util.Random; public class lab8 { public static void main(String[] args) { final int POINT = 2; int x = 0, y = 0; int miss = 0, hit = 0; double est = 0; ConsoleReader cin = new ConsoleReader(System.in); System.out.print("What is the X-Coordinate of the Circle: "); int xcir = cin.readInt(); System.out.print("What is the Y-Coordinate of the Circle: "); int ycir = cin.readInt(); System.out.print("Enter Circle Radius: "); int rad = cin.readInt(); System.out.print("Enter How Many Darts You Want To Throw: "); int n = cin.readInt(); double num = n; Random generator = new Random(); for(long i = 1; i <= n; ++i) { x = 1 + generator.nextInt(POINT); y = 1 + generator.nextInt(POINT); //System.out.print("Dart Location: ("+x+","+y+") "); if(getLength(x,y,xcir,ycir,rad) > 1) { //System.out.println("You Missed: " + getLength(x,y,xcir,ycir,rad)); miss++; } else { hit++; if(getLength(x,y,xcir,ycir,rad) == 0) { //System.out.println("Bulls Eye: " + getLength(x,y,xcir,ycir,rad)); } else { //System.out.println("Good Shot: " + getLength(x,y,xcir,ycir,rad)); } } if(i%1000 == 0) { if(i%20000 == 0) { est = hit/num; } else { dartEST(hit,i,num); } } } double absValue = Math.abs(est - (Math.PI/4)); System.out.println("\n\n\n"); System.out.println("The computer's value of PI from Math.PI is: " + Math.PI/4); System.out.println("The absolute value difference between Math.PI and my final estimated value for PI is: " + absValue); System.out.println("# of misses: " + miss); System.out.println("# of hits : " + hit); } public static double getLength(int x1, int y1, int xc1, int yc1, int rad1) { double length = Math.sqrt(Math.pow(xc1-x1,2) + Math.pow(yc1-y1,2)); return length; } public static void dartEST(int h1, long i, double num) { System.out.println("After " + i + " darts, the estimate value for PI is: " + h1/num); } }