From f3a46cf7f1beabb63b087f6451bed760e0cd0ad5 Mon Sep 17 00:00:00 2001 From: Thomas Kristiansen Date: Wed, 8 Jan 2025 11:57:02 +0100 Subject: [PATCH] Finished tasks --- .../java/com/booleanuk/core/Exercise.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index d31a45c..ffdf02f 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -43,7 +43,9 @@ public ArrayList getFavouriteNumbers() { TODO: 1. Create a method named getSecondNumber that returns a whole number. It must return the second number contained in the list that is returned from getFavouriteNumbers */ - + public int getSecondNumber() { + return getFavouriteNumbers().get(1); + } /* @@ -55,15 +57,21 @@ public ArrayList getFavouriteNumbers() { Use the ArrayList's replaceAll method to iterate through the ArrayList and replace each value with its double https://www.programiz.com/java-programming/library/arraylist/replaceall */ - - + public ArrayList multiply(ArrayList numList, int num) { + for (int i = 0; i < numList.size(); i++) { + numList.set(i, numList.get(i) * num); + } + return numList; + } /* TODO: 3. Create a method named isEmpty that accepts one parameter: - A list of strings The method must return a boolean that indicates whether the provided list is empty or not */ - + public boolean isEmpty(ArrayList list) { + return list.isEmpty(); + } /* @@ -72,7 +80,10 @@ public ArrayList getFavouriteNumbers() { - A string The method must add the second parameter into the list provided and then return the list */ - + public ArrayList addIngredient(ArrayList list, String ingredient) { + list.add(ingredient); + return list; + } /* @@ -81,6 +92,10 @@ public ArrayList getFavouriteNumbers() { - A string The method must remove the second parameter from the list and then return the list */ + public ArrayList removeIngredient(ArrayList list, String ingredient) { + list.remove(ingredient); + return list; + } @@ -90,7 +105,9 @@ public ArrayList getFavouriteNumbers() { - A string The method must return a boolean that indicates whether the second parameter exists in the provided list */ - + public boolean containsIngredient(ArrayList list, String ingredient) { + return list.contains(ingredient); + } }