From fc940ec08da0c4c6b8c6cbfe3d6c119eb09ece58 Mon Sep 17 00:00:00 2001 From: hannaklh Date: Wed, 13 Aug 2025 12:17:02 +0200 Subject: [PATCH] exercise complete --- .../java/com/booleanuk/core/Exercise.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..a5ac7fb 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,7 +47,10 @@ 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 list = createPerson(); + return list.get(key); + } /* @@ -57,7 +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 list, String key) { + return list.containsKey(key); + } /* @@ -68,7 +73,10 @@ public HashMap createPerson() { or -1 if the string provided is not a key in the HashMap */ - +public int getValueOrDefault (HashMap list, String value) { + int i= list.getOrDefault(value, -1); + return i; +} /* TODO: 4. Complete the method below @@ -90,12 +98,17 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... - + ArrayList newList = new ArrayList<>(); + for (Integer number : numbers) { + if (map.containsKey(number)) { + newList.add(map.get(number)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return newList; } }