Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/booleanuk/core/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


}
46 changes: 40 additions & 6 deletions src/main/java/com/booleanuk/extension/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<sArr.length; i++) {
int m = Integer.parseInt(sArr[i]);
resultArr[i] = m * n;
}
return resultArr;
}


}
Loading