KaiquanMah commited on
Commit
fc4751c
·
verified ·
1 Parent(s): 367fcb1

Create 8. Return winnings

Browse files
Week 2: Methods, strings and lists/8. Return winnings ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In the casino's new slot machine, the payout is determined by multiplying the player's bet by a random multiplier.
2
+ Write the method
3
+
4
+ double getWin(double bet)
5
+
6
+ ...which returns the player's winnings. A predefined method in the program getMultiplier() returns the payout coefficient as an integer.
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+ import java.util.Random;
15
+
16
+ public class Test{
17
+ private static Random rnd;
18
+
19
+ public static void main(String[] args){
20
+ rnd = new Random();
21
+
22
+
23
+ double[] p = {100.0, 25.0, 5.50, 0.50};
24
+ for (double pa : p) {
25
+ System.out.println("Testing with parameter " + pa);
26
+ System.out.println("Winnings: " + getWin(pa));
27
+ System.out.println("");
28
+ }
29
+
30
+ }
31
+
32
+ public static int getMultiplier() {
33
+ return rnd.nextInt(5) + 1;
34
+ }
35
+
36
+
37
+
38
+
39
+ public static double getWin(double bet) {
40
+ double winnings = bet * getMultiplier();
41
+
42
+ return winnings;
43
+ }
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+ }
54
+
55
+
56
+
57
+
58
+
59
+
60
+ Testing with parameter 100.0
61
+ Winnings: 100.0
62
+
63
+ Testing with parameter 25.0
64
+ Winnings: 75.0
65
+
66
+ Testing with parameter 5.5
67
+ Winnings: 27.5
68
+
69
+ Testing with parameter 0.5
70
+ Winnings: 2.0
71
+
72
+