Spaces:
Running
Running
param != null
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104In 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 withnew 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}
Week 4: Writing classes/10. Lap times' set and get methodsAdded+104