From bd31fb832ed4e6a617e1e4ac794bf86223d2902d Mon Sep 17 00:00:00 2001 From: Magnus-droid Date: Wed, 8 Jan 2025 11:56:43 +0100 Subject: [PATCH] Solved task --- .../java/com/booleanuk/core/Exercise.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index d31a45c..e239115 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -2,7 +2,9 @@ import com.booleanuk.helpers.ExerciseBase; +import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.function.UnaryOperator; public class Exercise extends ExerciseBase { /* @@ -43,6 +45,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,7 +60,10 @@ 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 ints, int number) { + ints.replaceAll(n -> n * number); + return ints; + } /* @@ -64,6 +72,10 @@ public ArrayList getFavouriteNumbers() { The method must return a boolean that indicates whether the provided list is empty or not */ + public boolean isEmpty(ArrayList strings) { + return strings.isEmpty(); + } + /* @@ -72,7 +84,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 ingredients, String toAdd) { + ingredients.add(toAdd); + return ingredients; + } /* @@ -81,9 +96,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 ingredients, String toRemove) { + ingredients.remove(toRemove); + return ingredients; + } /* TODO: 6. Create a method named containsIngredient that accepts two parameters in this order: - A list of strings @@ -91,6 +107,10 @@ public ArrayList getFavouriteNumbers() { The method must return a boolean that indicates whether the second parameter exists in the provided list */ + public boolean containsIngredient(ArrayList ingredients, String ingredient) { + return ingredients.contains(ingredient); + } + }