Skip to content
Merged
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
1 change: 1 addition & 0 deletions kata/8-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
- [Rock Paper Scissors!](rock-paper-scissors "5672a98bdbdd995fad00000f")
- [Sentence Smash](sentence-smash "53dc23c68a0c93699800041d")
- [Short Long Short](short-long-short "50654ddff44f800200000007")
- [simple calculator ](simple-calculator "5810085c533d69f4980001cf")
- [Simple Fun #1: Seats in Theater](simple-fun-number-1-seats-in-theater "588417e576933b0ec9000045")
- [Simple multiplication](simple-multiplication "583710ccaa6717322c000105")
- [Simple validation of a username with regex](simple-validation-of-a-username-with-regex "56a3f08aa9a6cc9b75000023")
Expand Down
19 changes: 19 additions & 0 deletions kata/8-kyu/simple-calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# [Simple calculator ](https://www.codewars.com/kata/simple-calculator "https://www.codewars.com/kata/5810085c533d69f4980001cf")

You are required to create a simple calculator that returns the result of addition, subtraction, multiplication or division of two numbers.

Your function will accept three arguments:
- The first and second argument should be numbers.
- The third argument should represent a sign indicating the operation to perform on these two numbers.

If the sign is not a valid sign, throw an IllegalArgumentException (Java) or ArgumentException (C#).

# Example:

```
arguments: 1, 2, "+"
should return 3

arguments: 1, 2, "&"
should throw an IllegalArgumentException (Java) or ArgumentException (C#)
```
11 changes: 11 additions & 0 deletions kata/8-kyu/simple-calculator/main/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Calculator {
static double calculate(double a, double b, String op) {
return switch (op) {
case "+" -> a + b;
case "-" -> a - b;
case "*" -> a * b;
case "/" -> a / b;
default -> throw new IllegalArgumentException();
};
}
}
30 changes: 30 additions & 0 deletions kata/8-kyu/simple-calculator/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SolutionTest {
@ParameterizedTest
@CsvSource(textBlock = """
1, 2, +, 3
6, 2, +, 8
4, 3, -, 1
5, 5, *, 25
5, 4, /, 1.25
""")
void basicOperation(double a, double b, String op, double expected) {
assertEquals(expected, Calculator.calculate(a, b, op), 1e-3);
}

@ParameterizedTest
@CsvSource(textBlock = """
6, 2, &
3, 5, \\
5, 5, =
6, 3, '\t'
""")
void unsupportedOperation(double a, double b, String op) {
assertThrows(IllegalArgumentException.class, () -> Calculator.calculate(a, b, op));
}
}