From 58edf27a8e98c960a7c75776264cc741828754d6 Mon Sep 17 00:00:00 2001 From: Marcus Ikdal Date: Wed, 8 Jan 2025 13:00:44 +0100 Subject: [PATCH] completed --- .../java/com/booleanuk/core/Exercise.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..cfb18f9 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,6 +47,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,7 +60,10 @@ 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 s) { + return map.containsKey(s); + } + /* @@ -67,6 +73,13 @@ 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 s) { + Integer val = map.get(s); + if (val != null) + return val; + else + return -1; + } @@ -90,12 +103,17 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... + ArrayList phrase = new ArrayList<>(); + for (Integer n : numbers) { + if (map.containsKey(n)) + phrase.add(map.get(n)); + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return phrase; } }