From d1ea1c13a8a2197315d0815f044b3f8409e9d791 Mon Sep 17 00:00:00 2001 From: Marie Helene Hansen Date: Tue, 5 Aug 2025 14:53:17 +0200 Subject: [PATCH] task complete --- .../java/com/booleanuk/core/Exercise.java | 13 +++-- .../com/booleanuk/extension/Extension.java | 48 ++++++++++++++++++- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 7987028..b1337cd 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -51,20 +51,27 @@ public Exercise(int age) { Create a constructor that accepts both a String and an int as parameters, in that order, and assign the values provided to the name and age members */ - + public Exercise(String name, int age) { + this.name = name; + this.age = age; + } /* 2. Create a method named add that accepts two integers. The method should return the numbers added together. */ - + public int add(int numOne, int numTwo) { + return numOne + numTwo; + } /* 3. Create another method named add that accepts two Strings. The method should return the strings concatenated together with a space in between. */ - + public String add(String stringOne, String stringTwo) { + return stringOne + " " + stringTwo; + } } diff --git a/src/main/java/com/booleanuk/extension/Extension.java b/src/main/java/com/booleanuk/extension/Extension.java index 62b878f..e35707c 100644 --- a/src/main/java/com/booleanuk/extension/Extension.java +++ b/src/main/java/com/booleanuk/extension/Extension.java @@ -3,6 +3,28 @@ import com.booleanuk.helpers.ExtensionBase; public class Extension extends ExtensionBase { + + public float add(float float1, float float2) { + return float1 + float2; + } + + public double add(double d1, double d2) { + return d1 + d2; + } + + public float subtract(float f1, float f2) { + return f1-f2; + } + + public String subtract(String str, char c) { + String res = ""; + for (int i = 0; i < str.length(); i ++) { + if (str.charAt(i) != c) { + res += str.charAt(i); + } + } + return res; + } /* Implement the following methods: @@ -20,11 +42,35 @@ public class Extension extends ExtensionBase { as many times as the provided int separated by a comma. E.g. multiply("Hello", 3) -> "Hello,Hello,Hello" - 7. multiply, which accepts an array of Strings that each contain a number, and an int + + */ + public int multiply(int i1, int i2) { + return i1 * i2; + } + + public String multiply(String str, int i) { + String res = str + ","; + while (i > 2) { + res += str + ","; + i --; + } + res += str; + return res; + } + + /* + 7. multiply, which accepts an array of Strings that each contain a number, and an int The method should return an array of ints that contain the value of multiplying each String number by the provided int E.g. multiply(["2", "7", "3"], 3) -> [6, 21, 9] */ + public int[] multiply(String[] iString, int factor) { + int[] res = new int[iString.length]; + for (int i = 0; i < iString.length; i ++) { + res[i] = Integer.parseInt(iString[i]) * factor; + } + return res; + } }