-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinFlip.java
More file actions
76 lines (66 loc) · 2.18 KB
/
CoinFlip.java
File metadata and controls
76 lines (66 loc) · 2.18 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* The CoinFlip class simulates flipping a coin and stores the results of 100 flips.
*/
public class CoinFlip {
// Boolean array to store the results of coin flips
private static boolean[] coinFlips = new boolean[100];
/**
* flipIt method flips the coin and stores result using the store method.
*/
public static void flipIt() {
for (int i = 0; i < coinFlips.length; i++) {
boolean flipResult = Math.random() > .5;
store(i, flipResult);
}
}
/**
* The store method stores result of the coin flips.
* @param index The index in the array where the result will be stored.
* @param flipResult The result of the coin flip.
*/
private static void store(int index, boolean flipResult) {
coinFlips[index] = flipResult;
}
/**
* The display method displays the results of the coin flips in 10 rows and 10 columns,
* and reports the total number of heads, tails, and the longest run of heads.
*/
public static void display() {
int headsCount = 0;
int tailsCount = 0;
int longestRun = 0;
int currentRun = 0;
for (int i = 0; i < coinFlips.length; i++) {
if (i % 10 == 0) {
System.out.println();
}
if (coinFlips[i]) {
headsCount++;
System.out.print("heads ");
currentRun++;
} else {
tailsCount++;
System.out.print("tails ");
currentRun = 0;
}
int j = i;
while (j < coinFlips.length && coinFlips[j]) {
currentRun++;
j++;
}
if (currentRun > longestRun) {
longestRun = currentRun;
}
}
System.out.println("\nHeads: " + headsCount);
System.out.println("Tails: " + tailsCount);
System.out.println("Longest run of heads is: " + longestRun);
}
/**
* The main method demonstrates the usage of the CoinFlip class methods.
*/
public static void main(String[] args) {
CoinFlip.flipIt();
CoinFlip.display();
}
}