From 46d4982a4ab7e667784b3b3c0f24fe631965d96d Mon Sep 17 00:00:00 2001 From: Magnus-droid Date: Wed, 8 Jan 2025 12:58:13 +0100 Subject: [PATCH] Solved tasks --- .../java/com/booleanuk/core/Exercise.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..6993773 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -2,6 +2,7 @@ import com.booleanuk.helpers.ExerciseBase; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; @@ -47,7 +48,9 @@ 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) { + return createPerson().get(key); + } /* @@ -57,6 +60,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 key) { + return map.containsKey(key); + } @@ -67,9 +73,12 @@ 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 key) { + if (map.containsKey(key)) { + return map.get(key); + } + return -1; + } /* TODO: 4. Complete the method below Example input & output: @@ -90,12 +99,15 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... - - - + ArrayList result = new ArrayList<>(); + for (int num : numbers) { + if (map.containsKey(num)) { + result.add(map.get(num)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return result; } }