From 14f771c385eaab6154f0eda6d87df926085be183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20S=C3=B6nnergaard?= Date: Wed, 8 Jan 2025 12:57:08 +0100 Subject: [PATCH] Complete core exercises --- .../java/com/booleanuk/core/Exercise.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..fbd2596 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -2,8 +2,10 @@ import com.booleanuk.helpers.ExerciseBase; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; public class Exercise extends ExerciseBase { /* @@ -48,7 +50,9 @@ public HashMap createPerson() { in the createPerson method */ - + public String getValue(String key) { + return createPerson().get(key); + } /* TODO: 2. Create a method named hasKey that accepts two parameters: @@ -58,7 +62,9 @@ public HashMap createPerson() { in the provided HashMap */ - + public boolean hasKey(HashMap map, String key) { + return map.containsKey(key); + } /* TODO: 3. Create a method named getValueOrDefault that accepts two parameters: @@ -68,7 +74,13 @@ public HashMap createPerson() { or -1 if the string provided is not a key in the HashMap */ - + public int getValueOrDefault(HashMap map, String key) { + if (map.containsKey(key)) { + return map.get(key); + } else { + return -1; + } + } /* TODO: 4. Complete the method below @@ -90,12 +102,16 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... - - + ArrayList secret = new ArrayList<>(); + for (int n : numbers) { + if (map.containsKey(n)) { + secret.add(map.get(n)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return secret; } }