File size: 2,801 Bytes
788fc85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
In the attached program, the class 'Results' is defined.

The class attribute 'laptimes' lacks set and get methods.
Implement the methods in the class. 
The set method can be set to any value except null.


import java.util.Random;
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        final Random r = new Random();
        

        System.out.println("Testing class...");

        double[] additions = {0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 0.9, 0.99, 0.999};

        HashMap<String, Double> lapTimes = new HashMap<>();
        lapTimes.put("Jack Smith", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
        lapTimes.put("Harry Johnson", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
        lapTimes.put("Oliver Williams", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
        lapTimes.put("George Brown", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);
        lapTimes.put("Charlie Jones", r.nextInt(10) + 1 + additions[r.nextInt(additions.length)]);

        Results results = new Results("Montsa", new HashMap<>());

        System.out.println("Created Results with");
        System.out.println("new Results(\"Montsa\", new HashMap<String, Double>())");

        System.out.println("Setting laptimes as " + lapTimes);
        results.setLaptimes(lapTimes);
        System.out.println("Laptimes now:" + results.getLaptimes());
        System.out.println("");

        System.out.println("Setting laptimes null...");
        results.setLaptimes(null);
        System.out.println("Laptimes now:" + results.getLaptimes());
        System.out.println("");
    }
}

class Results {
    // PRIVATE ATTRIBUTES
    private String race;
    private HashMap<String, Double> laptimes;

    // CONSTRUCTOR
    public Results(String race, HashMap<String, Double> laptimes) {
        this.race = race;
        this.laptimes = laptimes;
    }


    // GET, SET METHODS
    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }


    public HashMap<String, Double> getLaptimes() {
        return this.laptimes;
    }

    public void setLaptimes(HashMap<String, Double> laptimes) {
        if (laptimes != null) {
            this.laptimes = laptimes;
        }
    }











    
}







Testing class...
Created Results with
new Results("Montsa", new HashMap())
Setting laptimes as {Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}
Laptimes now:{Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}

Setting laptimes null...
Laptimes now:{Harry Johnson=9.75, George Brown=3.999, Jack Smith=10.2, Charlie Jones=7.99, Oliver Williams=6.4}