From 7828536d1afa3333d0b8ab093d6f99ae2f648fac Mon Sep 17 00:00:00 2001 From: David Stern Date: Tue, 12 Aug 2025 14:27:21 +0200 Subject: [PATCH] David Stern --- .../java/com/booleanuk/core/Exercise.java | 34 ++++++++++++------- .../com/booleanuk/extension/Extension.java | 26 +++++++++++++- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 0f97fe4..154e423 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -11,35 +11,45 @@ public class Exercise { public void stepOne() { // TODO: 1. Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array - - + numsZeroToThree = new int[4]; + for (int i = 0; i < 4; i++) { + numsZeroToThree[i] = i; + } } public void stepTwo() { // TODO: 2. Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array - - + numsFiveToTen = new int[6]; + for (int i = 5; i < 11; i++) { + numsFiveToTen[i - 5] = i; + } } public void stepThree() { // TODO: 3. Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array - - + countdown = new int[4]; + for (int i = 3; i >= 0; i--) { + countdown[3 - i] = i; + } } public boolean stepFour(int num) { // TODO: 6. Write a for loop that checks if num is in the favouriteNumbers array - - - + for (int i = 0; i < favouriteNumbers.length; i++) { + if (favouriteNumbers[i] == num) { + return true; + } + } return false; } public boolean stepFive(String hobby) { // TODO 5. Write a for loop that checks if the hobby String is in the myHobbies array - - - + for (int i = 0; i < myHobbies.length; i++) { + if (myHobbies[i].equals(hobby)) { + return true; + } + } return false; } } diff --git a/src/main/java/com/booleanuk/extension/Extension.java b/src/main/java/com/booleanuk/extension/Extension.java index 8b9d4a6..dbaac1e 100644 --- a/src/main/java/com/booleanuk/extension/Extension.java +++ b/src/main/java/com/booleanuk/extension/Extension.java @@ -5,8 +5,32 @@ public class Extension { public void stepOne() { // TODO: 1. Write a for loop that adds all the even numbers between 0 and 6 (0, 2, 4, 6) to evenNums + int countEven = 0; + int evenAdded = 0; + for (int i = 0; i < 7; i++) { + if (i % 2 == 0) { + countEven++; + } + } + numsEven = new int[countEven]; - + for (int i = 0; i < 7; i++) { + if (i % 2 == 0) { + numsEven[evenAdded++] = i; + } + } + + /* + Alternative: + List evenNums = new ArrayList<>; + for (int i = 0; i < 7; i++) { + if (i % 2 == 0) { + evenNums.add(i); + } + } + + numsEven = evenNums.stream.mapToInt(Integer::intValue).toArray(); + */ } }