-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpgave3.java
76 lines (57 loc) · 1.88 KB
/
Opgave3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Hjemmeopgave2;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Opgave3 {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
//Gets user input
int nReps = getInput();
//Saves the number of times the needle crosses a line in a variable
int nOfCrosses = needleThrows(nReps);
//Prints out the estimated value of PI
System.out.println(nReps + " / " + nOfCrosses + " = " + (double)nReps/nOfCrosses);
}
public static int getInput() {
int reps=0;
System.out.print("Enter number of Iterations: ");
//Throws an error if a non-integer is recieved as input
try {
reps = scanner.nextInt();
}
catch(InputMismatchException ime) {
inputError();
reps = getInput();
}
//Throws an error if the received integer is zero or negative
if(reps <= 0) {
inputError();
reps = getInput();
}
return reps;
}
public static int needleThrows(int repetitions) {
int cases=0;
for(int i=0; i<repetitions; i++) {
//Determines a new midpoint and angle for the needle for every throw
double needleMiddle = 2*Math.random();
double needleAngle = Math.PI*Math.random();
//Calculates the ends of the needles
double needleEnd1 = needleMiddle + Math.sin(needleAngle)/2;
double needleEnd2 = needleMiddle - Math.sin(needleAngle)/2;
//Checks if needle crossed a line
Boolean crossed = (needleEnd1 > 2 || needleEnd2 < 0) ? true : false;
if(crossed){cases++;}
}
//Insures that the method never returns 0
if(cases == 0) {cases++;}
return cases;
}
public static void inputError() {
//Error message
System.out.println("\n======================================");
System.out.println("ERROR: PLEASE ENTER A POSITIVE INTEGER");
System.out.println("======================================\n");
//Clears the next line
scanner.nextLine();
}
}