KaiquanMah commited on
Commit
788fc85
·
verified ·
1 Parent(s): 88117e3

param != null

Browse files
Week 4: Writing classes/10. Lap times' set and get methods ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In the attached program, the class 'Results' is defined.
2
+
3
+ The class attribute 'laptimes' lacks set and get methods.
4
+ Implement the methods in the class.
5
+ The set method can be set to any value except null.
6
+
7
+
8
+ import java.util.Random;
9
+ import java.util.HashMap;
10
+
11
+ public class Test {
12
+ public static void main(String[] args) {
13
+ final Random r = new Random();
14
+
15
+
16
+ System.out.println("Testing class...");
17
+
18
+ double[] additions = {0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 0.9, 0.99, 0.999};
19
+
20
+ HashMap<String, Double> lapTimes = new HashMap<>();
21
+ lapTimes.put("Jack Smith", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
22
+ lapTimes.put("Harry Johnson", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
23
+ lapTimes.put("Oliver Williams", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
24
+ lapTimes.put("George Brown", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
25
+ lapTimes.put("Charlie Jones", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
26
+
27
+ Results results = new Results("Montsa", new HashMap<>());
28
+
29
+ System.out.println("Created Results with");
30
+ System.out.println("new Results(\"Montsa\", new HashMap<String, Double>())");
31
+
32
+ System.out.println("Setting laptimes as " + lapTimes);
33
+ results.setLaptimes(lapTimes);
34
+ System.out.println("Laptimes now:" + results.getLaptimes());
35
+ System.out.println("");
36
+
37
+ System.out.println("Setting laptimes null...");
38
+ results.setLaptimes(null);
39
+ System.out.println("Laptimes now:" + results.getLaptimes());
40
+ System.out.println("");
41
+ }
42
+ }
43
+
44
+ class Results {
45
+ // PRIVATE ATTRIBUTES
46
+ private String race;
47
+ private HashMap<String, Double> laptimes;
48
+
49
+ // CONSTRUCTOR
50
+ public Results(String race, HashMap<String, Double> laptimes) {
51
+ this.race = race;
52
+ this.laptimes = laptimes;
53
+ }
54
+
55
+
56
+ // GET, SET METHODS
57
+ public String getRace() {
58
+ return race;
59
+ }
60
+
61
+ public void setRace(String race) {
62
+ this.race = race;
63
+ }
64
+
65
+
66
+ public HashMap<String, Double> getLaptimes() {
67
+ return this.laptimes;
68
+ }
69
+
70
+ public void setLaptimes(HashMap<String, Double> laptimes) {
71
+ if (laptimes != null) {
72
+ this.laptimes = laptimes;
73
+ }
74
+ }
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+ }
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+ Testing class...
96
+ Created Results with
97
+ new Results("Montsa", new HashMap())
98
+ Setting laptimes as {Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}
99
+ Laptimes now:{Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}
100
+
101
+ Setting laptimes null...
102
+ Laptimes now:{Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}
103
+
104
+