From 550fdbe2d720313ce8580bac1d06375fcd59a2c3 Mon Sep 17 00:00:00 2001 From: Petter Karstensen Date: Wed, 10 Jan 2024 14:04:42 +0100 Subject: [PATCH 1/2] 3/5 exercises --- src/main/java/com/booleanuk/core/Exercise.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index f4aec33..c24dfde 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -22,7 +22,7 @@ public int[] one() { // E.g. The first value is at index 0, the second at index 1, the third at index 3. // 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 @@ -37,7 +37,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 @@ -48,13 +48,12 @@ 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 = {}; - return dummyArray; + return cars; } public int four() { From 79557d1f62a9b9088c1994d643335b9dd094dbc5 Mon Sep 17 00:00:00 2001 From: Petter Karstensen Date: Wed, 10 Jan 2024 15:05:09 +0100 Subject: [PATCH 2/2] 5/5 exercises --- src/main/java/com/booleanuk/core/Exercise.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index c24dfde..d079e7f 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -1,5 +1,7 @@ package com.booleanuk.core; +import java.util.Arrays; + public class Exercise { // The block of code below is a method definition. So far, you've been using methods created by other people // such as .length() and .charAt(n) @@ -63,7 +65,7 @@ public int four() { // sum of every number in the numbers array // WRITE YOUR CODE BETWEEN THIS LINE... - int result = 0; + int result = Arrays.stream(numbers).sum(); // ... AND THIS LINE @@ -74,12 +76,11 @@ 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 = {}; - return dummyArray; + return floats; } }