From bfba139246473b8bbc74dec755384aeeab6a88c8 Mon Sep 17 00:00:00 2001 From: Magnus Hissingby Date: Wed, 6 Aug 2025 09:36:23 +0200 Subject: [PATCH] Task arrays --- .../java/com/booleanuk/core/Exercise.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 4d002b4..340550e 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -22,9 +22,7 @@ public int[] one() { // E.g. The first value is at index 0, the second at index 1, the third at index 2. // Using an index, change the number 17 in the numbers array to 68 // WRITE YOUR CODE BETWEEN THIS LINE... - - - + numbers[2] = 68; // ... AND THIS LINE return numbers; @@ -37,8 +35,7 @@ public String two() { // teacher contained in the teachers array // WRITE YOUR CODE BETWEEN THIS LINE... - String teacher = ""; - + String teacher = teachers[3]; // ... AND THIS LINE return teacher; @@ -47,13 +44,11 @@ public String two() { public String[] three() { // 3. Create a string array named cars that contains three names of car manufacturers: Audi, BMW and Dodge // WRITE YOUR CODE BETWEEN THIS LINE... - - - + String[] cars = {"Audi", "BMW", "Dodge"}; // ... AND THIS LINE // Then change the code below to remove the dummyArray completely and return the cars array you created above instead. - String[] dummyArray = {}; + String[] dummyArray = cars; return dummyArray; } @@ -65,6 +60,9 @@ public int four() { // WRITE YOUR CODE BETWEEN THIS LINE... int result = 0; + for (int k = 0; k <= numbers.length-1; k++){ + result += numbers[k]; + } // ... AND THIS LINE @@ -75,12 +73,12 @@ public float[] five() { // 5. Create an array called floats that contains 3 floating point numbers: 9.62, 23.17 and 3.14 // WRITE YOUR CODE BETWEEN THIS LINE... - + float[] floats = {9.62F, 23.17F, 3.14F}; // ... AND THIS LINE // Then change the code below to remove the dummyArray completely and return the floats array you created instead - float[] dummyArray = {}; + float[] dummyArray = floats; return dummyArray; } }