From a8f6f517933f71254277551a409b933dba044b01 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Aug 2025 14:30:24 +0200 Subject: [PATCH 1/2] exercise finished --- .../java/com/booleanuk/core/Exercise.java | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..2cddc97 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,24 @@ 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) { + if (map.containsKey(theKey)){ + return map.get(theKey); + } + else{ + return -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 +96,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; } } From 359a803670ef3909a3fd5d046bbd0daf442c31f1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Aug 2025 15:00:57 +0200 Subject: [PATCH 2/2] fixed return on todo 3 --- src/main/java/com/booleanuk/core/Exercise.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 2cddc97..a31a6ed 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -70,12 +70,8 @@ public boolean hasKey(HashMap map, String theKey) { or -1 if the string provided is not a key in the HashMap */ public int getValueOrDefault(HashMap map, String theKey) { - if (map.containsKey(theKey)){ - return map.get(theKey); - } - else{ - return -1; - } + return map.getOrDefault(theKey, -1); + } /*