-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1for2018.java
More file actions
52 lines (47 loc) · 1.47 KB
/
P1for2018.java
File metadata and controls
52 lines (47 loc) · 1.47 KB
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
public class P1for2018{
public static void main(String [] args){
FrogSimulation sim = new FrogSimulation(24, 5);
int totalSteps = sim.hopDistance();
boolean result = sim.simulate(totalSteps);
System.out.println(result);
int num = (int)(Math.random() * (totalSteps + 1));
sim.runSimulation(num, totalSteps, result);
}
}
class FrogSimulation {
private int goalDistance;
private int maxHops;
private int[] steps;
public FrogSimulation(int goalDistance, int maxHops) {
this.goalDistance = goalDistance;
this.maxHops = maxHops;
this.steps = new int[]{5, 7, -15, 8, 60};
}
public int hopDistance() {
int totalSteps = 0;
for (int i = 0; i < steps.length; i++) {
totalSteps += steps[i];
}
return totalSteps;
}
public boolean simulate(int totalSteps) {
int hops = 0;
for (int i = 0; i < steps.length; i++) {
hops++;
for (int j = 0; j <= i; j++) {
if (totalSteps < goalDistance || hops == maxHops || totalSteps < 0) {
return false;
}
}
}
return true;
}
public double runSimulation(int num, int totalSteps, boolean result) {
double k = 0;
if (result == true) {
double proportion = (double) num / (double) totalSteps;
System.out.println(proportion);
}
return k;
}
}