From 4c5b33ad65d8330d68383551c9f7b9d567d3785f Mon Sep 17 00:00:00 2001 From: Moueed Ali Date: Wed, 6 Aug 2025 12:56:43 +0200 Subject: [PATCH] Exercise completed --- .../java/com/booleanuk/core/Exercise.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..b018186 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -48,6 +48,10 @@ public HashMap createPerson() { in the createPerson method */ + public String getValue(String key) { + HashMap map = createPerson(); + return map.get(key); + } /* @@ -58,6 +62,9 @@ public HashMap createPerson() { in the provided HashMap */ + public boolean hasKey(HashMap map, String str) { + return map.containsKey(str); + } /* @@ -67,6 +74,15 @@ 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)) { + int value = map.get(str); + System.out.println(value); + return value; + } else { + return -1; + } + } @@ -90,12 +106,20 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... + ArrayList lst = new ArrayList<>(); + for (int i = 0; i < numbers.size(); i++) { + int key = numbers.get(i); + if (map.containsKey(key)) { + String value = map.get(key); + lst.add(value); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return lst; } }