From e1d70a7c3b57f86c480dc39f0a4fd0d51bfda08a Mon Sep 17 00:00:00 2001 From: msg Date: Thu, 11 Jan 2024 13:21:06 +0100 Subject: [PATCH] Core 9/9 --- .../java/com/booleanuk/core/Exercise.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index d31a45c..10f988f 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -17,7 +17,7 @@ public class Exercise extends ExerciseBase { /* Take some time to read and understand the method below. - It creates an instance of an ArrayList that will contain Integer types - We define the data type that will be stored in a list using angled brackets: + We define the data type that will be stored in a list using angled brackets: ArrayList We can store any data type in a list, even our own custom classes. ArrayList @@ -43,8 +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); + } /* TODO: 2. Create a method named multiply that accepts two parameters in this order: @@ -55,16 +56,19 @@ 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 list, int num) { + list.replaceAll(e -> e * num); + return list; + } /* 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(); + } /* TODO: 4. Create a method named addIngredient that accepts two parameters in this order: @@ -72,8 +76,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 string) { + list.add(string); + return list; + } /* TODO: 5. Create a method named removeIngredient that accepts two parameters in this order: @@ -81,8 +87,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 string) { + list.remove(1); + return list; + } /* TODO: 6. Create a method named containsIngredient that accepts two parameters in this order: @@ -90,7 +98,7 @@ 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 string) { + return list.contains(string); + } }