From 0af26c762c243995e891f5fc24b613e14c181a66 Mon Sep 17 00:00:00 2001 From: Hanna Adenholm Date: Wed, 6 Aug 2025 13:27:15 +0200 Subject: [PATCH] Finished ex --- src/main/java/com/booleanuk/core/Exercise.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..9e211cc 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -48,7 +48,7 @@ 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 +58,7 @@ 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 +68,11 @@ 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); + return -1; + } /* TODO: 4. Complete the method below @@ -90,12 +94,17 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... + ArrayList phrase = new ArrayList<>(); + for(Integer num: numbers){ + if (map.containsKey(num)) + phrase.add(map.get(num)); + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return phrase; } }