diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 7987028..4f8f192 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 a, int b) { + return a + b; + } /* 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 a, String b) { + return a + " " + b; + } } diff --git a/src/main/java/com/booleanuk/extension/Extension.java b/src/main/java/com/booleanuk/extension/Extension.java index 62b878f..82874ad 100644 --- a/src/main/java/com/booleanuk/extension/Extension.java +++ b/src/main/java/com/booleanuk/extension/Extension.java @@ -6,25 +6,59 @@ public class Extension extends ExtensionBase { /* Implement the following methods: - 1. add, which accepts two floats and returns a float (both floats added together) + 1. add, which accepts two floats and returns a float (both floats added together) */ + public float add(float a, float b) { + return a + b; + } + /* + + 2. add, which accepts two doubles and returns a double (both doubles added together) */ + public double add(double a, double b) { + return a + b; + } + /* - 2. add, which accepts two doubles and returns a double (both doubles added together) + 3. subtract, which accepts two floats and returns a float (first float minus second float) */ + public float subtract(float a, float b) { + return a - b; + } + /* - 3. subtract, which accepts two floats and returns a float (first float minus second float) + 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed */ + public String subtract(String s, char removeChar) { + return s.replaceAll(Character.toString(removeChar), ""); + } + /* - 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed + 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) */ + public int multiply(int a, int b) { + return a * b; + } - 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) + /* 6. multiply, which accepts a string and an int, and returns a string containing the provided string as many times as the provided int separated by a comma. E.g. - multiply("Hello", 3) -> "Hello,Hello,Hello" + multiply("Hello", 3) -> "Hello,Hello,Hello" */ + public String multiply(String s, int n) { + return s + ("," + s).repeat(Math.max(0, n - 1)); + } + + /* 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[] sArr, int n) { + int[] resultArr = new int[sArr.length]; + for (int i=0; i