Skip to content

Commit fc5f229

Browse files
author
Your Name
committed
exercise finished
1 parent e19b26f commit fc5f229

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/main/java/com/booleanuk/core/Exercise.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,27 @@ public Exercise(int age) {
5151
Create a constructor that accepts both a String and an int as parameters, in that order, and assign the values
5252
provided to the name and age members
5353
*/
54-
54+
public Exercise(String name, int age) {
55+
this.name = name;
56+
this.age = age;
57+
}
5558

5659

5760
/*
5861
2. Create a method named add that accepts two integers. The method should return the numbers added together.
5962
*/
60-
63+
public int add(int a, int b) {
64+
return a + b;
65+
}
6166

6267

6368
/*
6469
3. Create another method named add that accepts two Strings. The method should return the strings concatenated
6570
together with a space in between.
6671
*/
67-
72+
public String add(String a, String b) {
73+
return a + " " + b;
74+
}
6875

6976

7077
}

src/main/java/com/booleanuk/extension/Extension.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,49 @@ public class Extension extends ExtensionBase {
2525
E.g.
2626
multiply(["2", "7", "3"], 3) -> [6, 21, 9]
2727
*/
28+
public float add(float a, float b) {
29+
return a + b;
30+
}
2831

32+
public double add(double a, double b) {
33+
return a + b;
34+
}
35+
36+
public float subtract(float a, float b) {
37+
return a - b;
38+
}
39+
40+
public String subtract(String input, char ch) {
41+
return input.replace(String.valueOf(ch), "");
42+
}
43+
44+
public int multiply(int a, int b) {
45+
return a * b;
46+
}
47+
48+
public String multiply(String str, int times) {
49+
if (times <= 0) return "";
50+
StringBuilder sb = new StringBuilder();
51+
for (int i = 0; i < times; i++) {
52+
sb.append(str);
53+
if (i < times - 1) {
54+
sb.append(",");
55+
}
56+
}
57+
return sb.toString();
58+
}
59+
60+
public int[] multiply(String[] numbers, int multiplier) {
61+
int[] result = new int[numbers.length];
62+
for (int i = 0; i < numbers.length; i++) {
63+
try {
64+
int num = Integer.parseInt(numbers[i]);
65+
result[i] = num * multiplier;
66+
} catch (NumberFormatException e) {
67+
result[i] = 0;
68+
}
69+
}
70+
return result;
71+
}
2972

3073
}

0 commit comments

Comments
 (0)