From 5dacd5814b806c106a6356e574b305ef895afa20 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 18 Nov 2025 04:56:14 +0000 Subject: [PATCH] [Sync Iteration] java/calculator-conundrum/1 --- .../1/src/main/java/CalculatorConundrum.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 solutions/java/calculator-conundrum/1/src/main/java/CalculatorConundrum.java diff --git a/solutions/java/calculator-conundrum/1/src/main/java/CalculatorConundrum.java b/solutions/java/calculator-conundrum/1/src/main/java/CalculatorConundrum.java new file mode 100644 index 0000000..a6aeecd --- /dev/null +++ b/solutions/java/calculator-conundrum/1/src/main/java/CalculatorConundrum.java @@ -0,0 +1,31 @@ +class CalculatorConundrum { + public String calculate(int operand1, int operand2, String operation) { + if (operation == null) + { + throw new IllegalArgumentException("Operation cannot be null"); + } + if(operation == "") + { + throw new IllegalArgumentException("Operation cannot be empty"); + } + int result ; + switch(operation){ + case "+" : result = operand1 + operand2; + break; + case "*" : result = operand1 * operand2; + break; + case "/" : + try{ + result = operand1 / operand2; + } + catch(ArithmeticException e){ + throw new IllegalOperationException("Division by zero is not allowed",e); + } + break; + default : + throw new IllegalOperationException("Operation '" + operation + "' does not exist"); + } + return operand1 + " " + operation + " " + operand2 + " = " + result; + + } +}