From 81784dc73c8782455dc1f08c8ecad1408b511fcc Mon Sep 17 00:00:00 2001 From: alinjo Date: Tue, 13 Aug 2024 15:16:50 +0200 Subject: [PATCH 1/2] Solved Java lists --- .../java/com/booleanuk/core/Exercise.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index d31a45c..3dde013 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -43,8 +43,12 @@ 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(){ + ArrayList list = getFavouriteNumbers(); + return list.get(1); + } /* TODO: 2. Create a method named multiply that accepts two parameters in this order: @@ -55,6 +59,12 @@ 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 WholeNum){ + + list.replaceAll(e -> e * WholeNum); + return list; + + } @@ -64,7 +74,9 @@ public ArrayList getFavouriteNumbers() { 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,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 list, String ingredient){ + list.add(ingredient); + return list; + } /* @@ -81,7 +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 list, String ingredient){ + list.remove(ingredient); + return list; + } /* @@ -90,7 +108,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); - +} } From 8b63d2957fb831bab0422a2f1713f3d7e25a94d8 Mon Sep 17 00:00:00 2001 From: alinjo Date: Tue, 13 Aug 2024 15:30:36 +0200 Subject: [PATCH 2/2] Solved Java lists --- src/main/java/com/booleanuk/core/Exercise.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 3dde013..aa37db6 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -35,7 +35,6 @@ public ArrayList getFavouriteNumbers() { list.add(42); list.add(360); list.add(120); - return list; }