From 60f3716fa22cbfe7e8839eecfab9842b60e216e6 Mon Sep 17 00:00:00 2001 From: Roderick Leito Date: Wed, 6 Aug 2025 13:28:27 +0200 Subject: [PATCH] Finished Ex --- .../java/com/booleanuk/core/Exercise.java | 27 ++++++++++++++----- 1 file changed, 20 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..48e4389 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 str){ + HashMap map = createPerson(); + return map.get(str); + } /* @@ -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 map, String str){ + return map.containsKey(str); + } /* @@ -67,7 +72,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 str){ + if(map.containsKey(str)){ + return map.get(str); + } + return -1; + } /* @@ -90,12 +100,15 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... - - - + ArrayList resultList = new ArrayList<>(); + for(int i = 0; i < numbers.size(); i++) { + if (map.containsKey(numbers.get(i))) { + resultList.add(map.get(numbers.get(i))); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return resultList; } }