From b47cd8e324cbee6a33d34862f26d055f0518170a Mon Sep 17 00:00:00 2001 From: Connor Stokes Date: Tue, 13 Aug 2024 17:31:27 +0100 Subject: [PATCH] passes tests --- .../java/com/booleanuk/core/Exercise.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..f139665 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -31,6 +31,7 @@ If we wanted to map a persons details (their first name, last name, occupation e - It returns the HashMap https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Map.html */ + public HashMap createPerson() { HashMap map = new HashMap<>(); @@ -47,7 +48,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 this.createPerson().get(key); + } /* @@ -58,6 +61,9 @@ public HashMap createPerson() { in the provided HashMap */ + public boolean hasKey(HashMap hashmap, String key) { + return hashmap.containsKey(key); + } /* @@ -67,7 +73,9 @@ 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 hashmap, String key) { + return hashmap.getOrDefault(key, -1); + } /* @@ -89,13 +97,17 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(7, "muse"); map.put(96, "nice"); // Write your code below this comment... - - - + ArrayList list = new ArrayList<>(); + for(int i=0; i < numbers.size();i++){ + int num = numbers.get(i); + if (map.containsKey(num)) { // Check if the map contains the number + list.add(map.get(num)); // Add the corresponding value to the list + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return list; } }