From 4a0b77c5de92ea9a06cd42a1d372a707ef8a77b2 Mon Sep 17 00:00:00 2001 From: Marie Helene Hansen Date: Wed, 6 Aug 2025 13:00:32 +0200 Subject: [PATCH] task complete --- .../java/com/booleanuk/core/Exercise.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..9837209 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -48,7 +48,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: @@ -57,7 +59,12 @@ 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 hm, String str) { + for (String k : hm.keySet()) { + if (k.equals(str)) return true; + } + return false; + } /* @@ -67,7 +74,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 hm, String str) { + if (hm.containsKey(str)) { + return hm.get(str); + } + return -1; + } /* @@ -89,13 +101,16 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(7, "muse"); map.put(96, "nice"); // Write your code below this comment... - - - + ArrayList res = new ArrayList<>(); + for (int n: numbers) { + if (map.containsKey(n)) { + res.add(map.get(n)); + } + } + return res; // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); } }