Skip to content

Commit a0d2f3b

Browse files
* docs: kata description * feat: kata/simple-calculator * refactor: no division by zero error guaranteed in input --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent f5c9741 commit a0d2f3b

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

kata/8-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
- [Rock Paper Scissors!](rock-paper-scissors "5672a98bdbdd995fad00000f")
181181
- [Sentence Smash](sentence-smash "53dc23c68a0c93699800041d")
182182
- [Short Long Short](short-long-short "50654ddff44f800200000007")
183+
- [simple calculator ](simple-calculator "5810085c533d69f4980001cf")
183184
- [Simple Fun #1: Seats in Theater](simple-fun-number-1-seats-in-theater "588417e576933b0ec9000045")
184185
- [Simple multiplication](simple-multiplication "583710ccaa6717322c000105")
185186
- [Simple validation of a username with regex](simple-validation-of-a-username-with-regex "56a3f08aa9a6cc9b75000023")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# [Simple calculator ](https://www.codewars.com/kata/simple-calculator "https://www.codewars.com/kata/5810085c533d69f4980001cf")
2+
3+
You are required to create a simple calculator that returns the result of addition, subtraction, multiplication or division of two numbers.
4+
5+
Your function will accept three arguments:
6+
- The first and second argument should be numbers.
7+
- The third argument should represent a sign indicating the operation to perform on these two numbers.
8+
9+
If the sign is not a valid sign, throw an IllegalArgumentException (Java) or ArgumentException (C#).
10+
11+
# Example:
12+
13+
```
14+
arguments: 1, 2, "+"
15+
should return 3
16+
17+
arguments: 1, 2, "&"
18+
should throw an IllegalArgumentException (Java) or ArgumentException (C#)
19+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface Calculator {
2+
static double calculate(double a, double b, String op) {
3+
return switch (op) {
4+
case "+" -> a + b;
5+
case "-" -> a - b;
6+
case "*" -> a * b;
7+
case "/" -> a / b;
8+
default -> throw new IllegalArgumentException();
9+
};
10+
}
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class SolutionTest {
8+
@ParameterizedTest
9+
@CsvSource(textBlock = """
10+
1, 2, +, 3
11+
6, 2, +, 8
12+
4, 3, -, 1
13+
5, 5, *, 25
14+
5, 4, /, 1.25
15+
""")
16+
void basicOperation(double a, double b, String op, double expected) {
17+
assertEquals(expected, Calculator.calculate(a, b, op), 1e-3);
18+
}
19+
20+
@ParameterizedTest
21+
@CsvSource(textBlock = """
22+
6, 2, &
23+
3, 5, \\
24+
5, 5, =
25+
6, 3, '\t'
26+
""")
27+
void unsupportedOperation(double a, double b, String op) {
28+
assertThrows(IllegalArgumentException.class, () -> Calculator.calculate(a, b, op));
29+
}
30+
}

0 commit comments

Comments
 (0)