diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..a31a6ed 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,9 +47,11 @@ public HashMap createPerson() { The method must return the value associated to the provided key from the HashMap created in the createPerson method */ + public String getValue(String key) { + HashMap map = createPerson(); - - + return map.get(key); + } /* TODO: 2. Create a method named hasKey that accepts two parameters: - A HashMap of String, String key value pairs @@ -57,9 +59,9 @@ public HashMap createPerson() { The method must return a boolean that represents whether the string provided exists as a key in the provided HashMap */ - - - + public boolean hasKey(HashMap map, String theKey) { + return map.containsKey(theKey); + } /* TODO: 3. Create a method named getValueOrDefault that accepts two parameters: - A HashMap of String, Integer key value pairs @@ -67,18 +69,20 @@ public HashMap createPerson() { The method must use the string provided to return the integer contained in the provided HashMap, or -1 if the string provided is not a key in the HashMap */ + public int getValueOrDefault(HashMap map, String theKey) { + return map.getOrDefault(theKey, -1); - + } /* - TODO: 4. Complete the method below - Example input & output: - . - input output - [42, 6712, 7] | ArrayList ["universe", "bass", "muse"] - [23, 19, 96, 23, 165] | ArrayList ["chicken", "nice", "chicken", "soup"] - [918, 71, 88] | ArrayList [] - */ + TODO: 4. Complete the method below + Example input & output: + . + input output + [42, 6712, 7] | ArrayList ["universe", "bass", "muse"] + [23, 19, 96, 23, 165] | ArrayList ["chicken", "nice", "chicken", "soup"] + [918, 71, 88] | ArrayList [] + */ public ArrayList buildSecretPhrase(ArrayList numbers) { // Do not modify the map HashMap map = new HashMap<>(); @@ -88,14 +92,18 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(6712, "bass"); map.put(7, "muse"); map.put(96, "nice"); - // Write your code below this comment... - - - - + // Write your code below this comment. + ArrayList result = new ArrayList<>(); + + for (Integer number : numbers) { + if (map.containsKey(number)) { + String word = map.get(number); + result.add(word); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return result; } }