From b9b1db70d1780d844b8d8083c7d58862c9ef65dd Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 11:40:52 +0300 Subject: [PATCH 1/6] Added new solution --- src/main/java/core/basesyntax/Calculator.java | 21 ++ src/main/java/core/basesyntax/HelloWorld.java | 8 - .../java/core/basesyntax/CalculatorTest.java | 255 ++++++++++++++++++ .../java/core/basesyntax/HelloWorldTest.java | 8 - 4 files changed, 276 insertions(+), 16 deletions(-) create mode 100644 src/main/java/core/basesyntax/Calculator.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/test/java/core/basesyntax/CalculatorTest.java delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java new file mode 100644 index 00000000..df2500b5 --- /dev/null +++ b/src/main/java/core/basesyntax/Calculator.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Calculator { + + public double calculate(double value1, double value2, char operation) { + switch (operation) { + case '+': return value1 + value2; + case '-': return value1 - value2; + case '*': return value1 * value2; + case '/': + if (value2 == 0) { + throw new ArithmeticException("Division by zero"); + } + return value1 / value2; + case '^': + return Math.pow(value1, value2); + default: + throw new IllegalArgumentException("Illegal argument operation"); + } + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782b..00000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -} diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java new file mode 100644 index 00000000..c6ce449b --- /dev/null +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -0,0 +1,255 @@ +package core.basesyntax; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class CalculatorTest { + + private static Calculator calculator; + private final double firstPositiveOperand = 11.13; + private final double secondPositiveOperand = 12.14; + private final double firstNegativeOperand = -21.23; + private final double secondNegativeOperand = -22.24; + private final int zeroOperand = 0; + + private final char addition = '+'; + private final char subtraction = '-'; + private final char division = '/'; + private final char multiplication = '*'; + private final char raisingToPower = '^'; + private final char illegalOperation = '$'; + + @BeforeAll + static void beforeAll() { + calculator = new Calculator(); + } + + @Test + void additionPositiveOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, addition); + double expected = firstPositiveOperand + secondPositiveOperand; + assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " + + expected + " but was " + actual); + } + + @Test + void additionNegativeOperands() { + double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, addition); + double expected = firstNegativeOperand + secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Addition of two negative operands should be " + + expected + " but was " + actual); + } + + @Test + void additionPositiveAndNegativeOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, addition); + double expected = firstPositiveOperand + secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" + + " operand should be " + expected + " but was " + actual); + } + + @Test + void additionOperationWithOneZeroOperand() { + double actual = calculator.calculate(zeroOperand, secondNegativeOperand, addition); + double expected = zeroOperand + secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative" + + " operand should be " + expected + " but was " + actual); + + actual = calculator.calculate(firstPositiveOperand, zeroOperand, addition); + expected = firstPositiveOperand + zeroOperand; + assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" + + " operand should be " + expected + " but was " + actual); + } + + @Test + void additionOperationWithMinOrMaxOperand() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, addition); + double expected = Double.MIN_VALUE + Double.MAX_VALUE; + assertEquals(expected, actual, "Test failed! Addition of min value and max value" + + " should be " + expected + " but was " + actual); + } + + @Test + void subtractionPositiveOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, + subtraction); + double expected = firstPositiveOperand - secondPositiveOperand; + assertEquals(expected, actual, "Test failed! Subtraction of two positive operands" + + " should be " + expected + " but was " + actual); + } + + @Test + void subtractionNegativeOperands() { + double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, + subtraction); + double expected = firstNegativeOperand - secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Subtraction of two negative operands" + + " should be " + expected + " but was " + actual); + } + + @Test + void subtractionPositiveAndNegativeOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, + subtraction); + double expected = firstPositiveOperand - secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a " + + "negative operand should be " + expected + " but was " + actual); + } + + @Test + void subtractionOperationWithOneZeroOperand() { + double actual = calculator.calculate(zeroOperand, secondNegativeOperand, subtraction); + double expected = zeroOperand - secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative" + + " operand should be " + expected + " but was " + actual); + + actual = calculator.calculate(firstPositiveOperand, zeroOperand, subtraction); + expected = firstPositiveOperand - zeroOperand; + assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a zero" + + " operand should be " + expected + " but was " + actual); + } + + @Test + void subtractionOperationWithMinOrMaxOperand() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, subtraction); + double expected = Double.MIN_VALUE - Double.MAX_VALUE; + assertEquals(expected, actual, "Test failed! Subtraction of min value and max value" + + " should be " + expected + " but was " + actual); + } + + @Test + void divisionPositiveOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, division); + double expected = firstPositiveOperand / secondPositiveOperand; + assertEquals(expected, actual, "Test failed! Division of two positive operands should be " + + expected + " but was " + actual); + } + + @Test + void divisionNegativeOperands() { + double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, division); + double expected = firstNegativeOperand / secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Division of two negative operands should be " + + expected + " but was " + actual); + } + + @Test + void divisionPositiveAndNegativeOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, division); + double expected = firstPositiveOperand / secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" + + " operand should be " + expected + " but was " + actual); + } + + @Test + void divisionOperationWithOneZeroOperand() { + double actual = calculator.calculate(zeroOperand, secondNegativeOperand, division); + double expected = zeroOperand / secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Division of a zero operand and a negative" + + " operand should be " + expected + " but was " + actual); + + assertThrows(ArithmeticException.class, () -> + calculator.calculate(firstPositiveOperand, zeroOperand, division), + "Test failed! Division by zero must throw the Arithmetic exception."); + } + + @Test + void divisionOperationWithMinOrMaxOperand() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, division); + double expected = Double.MIN_VALUE / Double.MAX_VALUE; + assertEquals(expected, actual, "Test failed! Division of min value and max value" + + " should be " + expected + " but was " + actual); + } + + @Test + void multiplicationPositiveOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, + multiplication); + double expected = firstPositiveOperand * secondPositiveOperand; + assertEquals(expected, actual, "Test failed! Multiplication of two positive operands" + + " should be " + expected + " but was " + actual); + } + + @Test + void multiplicationNegativeOperands() { + double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, + multiplication); + double expected = firstNegativeOperand * secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Multiplication of two negative operands" + + " should be " + expected + " but was " + actual); + } + + @Test + void multiplicationPositiveAndNegativeOperands() { + double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, + multiplication); + double expected = firstPositiveOperand * secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" + + " a negative operand should be " + expected + " but was " + actual); + } + + @Test + void multiplicationOperationWithOneZeroOperand() { + double actual = calculator.calculate(zeroOperand, secondNegativeOperand, multiplication); + double expected = zeroOperand * secondNegativeOperand; + assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and" + + " a negative operand should be " + expected + " but was " + actual); + + actual = calculator.calculate(firstPositiveOperand, zeroOperand, multiplication); + expected = firstPositiveOperand * zeroOperand; + assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" + + " a zero operand should be " + expected + " but was " + actual); + } + + @Test + void multiplicationOperationWithMinOrMaxOperand() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, multiplication); + double expected = Double.MIN_VALUE * Double.MAX_VALUE; + assertEquals(expected, actual, "Test failed! Multiplication of min value and max value" + + " should be " + expected + " but was " + actual); + } + + @Test + void raisingToPositivePower() { + double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, + raisingToPower); + double expected = Math.pow(firstPositiveOperand, secondPositiveOperand); + assertEquals(expected, actual, "Test failed! Raising to positive power should be " + + expected + " but was " + actual); + } + + @Test + void raisingToNegativePower() { + double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, + raisingToPower); + double expected = Math.pow(firstNegativeOperand, secondNegativeOperand); + assertEquals(expected, actual, "Test failed! Raising to negative power should be " + + expected + " but was " + actual); + } + + @Test + void raisingToZeroPower() { + double actual = calculator.calculate(firstPositiveOperand, zeroOperand, raisingToPower); + double expected = Math.pow(firstPositiveOperand, zeroOperand); + assertEquals(expected, actual, "Test failed! Raising to zero power" + + " should be " + expected + " but was " + actual); + } + + @Test + void raisingZeroToPower() { + double actual = calculator.calculate(zeroOperand, secondNegativeOperand, raisingToPower); + double expected = Math.pow(zeroOperand, secondNegativeOperand); + assertEquals(expected, actual, "Test failed! Raising zero to negative power " + + " should be " + expected + " but was " + actual); + } + + @Test + void calculate_illegalOperation_notOk() { + assertThrows(IllegalArgumentException.class, () -> + calculator.calculate(firstPositiveOperand, secondPositiveOperand, + illegalOperation)); + } +} diff --git a/src/test/java/core/basesyntax/HelloWorldTest.java b/src/test/java/core/basesyntax/HelloWorldTest.java deleted file mode 100644 index 9a90d822..00000000 --- a/src/test/java/core/basesyntax/HelloWorldTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorldTest { - -} From 41850640cc7eb805610fbe8b157ecebfc82c4f7a Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 14:29:56 +0300 Subject: [PATCH 2/6] Added new solution (with student requires) --- src/main/java/core/basesyntax/Calculator.java | 9 +- .../java/core/basesyntax/CalculatorTest.java | 196 +++++++++--------- 2 files changed, 104 insertions(+), 101 deletions(-) diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index df2500b5..d4ddf1e9 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -4,9 +4,12 @@ public class Calculator { public double calculate(double value1, double value2, char operation) { switch (operation) { - case '+': return value1 + value2; - case '-': return value1 - value2; - case '*': return value1 * value2; + case '+': + return value1 + value2; + case '-': + return value1 - value2; + case '*': + return value1 * value2; case '/': if (value2 == 0) { throw new ArithmeticException("Division by zero"); diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index c6ce449b..5b9ed7af 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -9,18 +9,18 @@ class CalculatorTest { private static Calculator calculator; - private final double firstPositiveOperand = 11.13; - private final double secondPositiveOperand = 12.14; - private final double firstNegativeOperand = -21.23; - private final double secondNegativeOperand = -22.24; - private final int zeroOperand = 0; - - private final char addition = '+'; - private final char subtraction = '-'; - private final char division = '/'; - private final char multiplication = '*'; - private final char raisingToPower = '^'; - private final char illegalOperation = '$'; + private static final double FIRST_POSITIVE_OPERAND = 11.13; + private static final double SECOND_POSITIVE_OPERAND = 12.14; + private static final double FIRST_NEGATIVE_OPERAND = -21.23; + private static final double SECOND_NEGATIVE_OPERAND = -22.24; + private static final double ZERO_OPERAND = 0.0; + + private static final char ADDITION = '+'; + private static final char SUBTRACTION = '-'; + private static final char DIVISION = '/'; + private static final char MULTIPLICATION = '*'; + private static final char RAISING_TO_POWER = '^'; + private static final char ILLEGAL_OPERATION = '$'; @BeforeAll static void beforeAll() { @@ -28,228 +28,228 @@ static void beforeAll() { } @Test - void additionPositiveOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, addition); - double expected = firstPositiveOperand + secondPositiveOperand; + void calculate_additionPositiveOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ADDITION); + double expected = FIRST_POSITIVE_OPERAND + SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " + expected + " but was " + actual); } @Test - void additionNegativeOperands() { - double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, addition); - double expected = firstNegativeOperand + secondNegativeOperand; + void calculate_additionNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double expected = FIRST_NEGATIVE_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of two negative operands should be " + expected + " but was " + actual); } @Test - void additionPositiveAndNegativeOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, addition); - double expected = firstPositiveOperand + secondNegativeOperand; + void calculate_additionPositiveAndNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double expected = FIRST_POSITIVE_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" + " operand should be " + expected + " but was " + actual); } @Test - void additionOperationWithOneZeroOperand() { - double actual = calculator.calculate(zeroOperand, secondNegativeOperand, addition); - double expected = zeroOperand + secondNegativeOperand; + void calculate_additionOperationWithOneZeroOperand_Ok() { + double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double expected = ZERO_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative" + " operand should be " + expected + " but was " + actual); - actual = calculator.calculate(firstPositiveOperand, zeroOperand, addition); - expected = firstPositiveOperand + zeroOperand; + actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, ADDITION); + expected = FIRST_POSITIVE_OPERAND + ZERO_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" + " operand should be " + expected + " but was " + actual); } @Test - void additionOperationWithMinOrMaxOperand() { - double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, addition); + void calculate_additionOperationWithMinOrMaxOperand_Ok() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, ADDITION); double expected = Double.MIN_VALUE + Double.MAX_VALUE; assertEquals(expected, actual, "Test failed! Addition of min value and max value" + " should be " + expected + " but was " + actual); } @Test - void subtractionPositiveOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, - subtraction); - double expected = firstPositiveOperand - secondPositiveOperand; + void calculate_subtractionPositiveOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, + SUBTRACTION); + double expected = FIRST_POSITIVE_OPERAND - SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Subtraction of two positive operands" + " should be " + expected + " but was " + actual); } @Test - void subtractionNegativeOperands() { - double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, - subtraction); - double expected = firstNegativeOperand - secondNegativeOperand; + void calculate_subtractionNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, + SUBTRACTION); + double expected = FIRST_NEGATIVE_OPERAND - SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Subtraction of two negative operands" + " should be " + expected + " but was " + actual); } @Test - void subtractionPositiveAndNegativeOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, - subtraction); - double expected = firstPositiveOperand - secondNegativeOperand; + void calculate_subtractionPositiveAndNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, + SUBTRACTION); + double expected = FIRST_POSITIVE_OPERAND - SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a " + "negative operand should be " + expected + " but was " + actual); } @Test - void subtractionOperationWithOneZeroOperand() { - double actual = calculator.calculate(zeroOperand, secondNegativeOperand, subtraction); - double expected = zeroOperand - secondNegativeOperand; + void calculate_subtractionOperationWithOneZeroOperand_Ok() { + double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, SUBTRACTION); + double expected = ZERO_OPERAND - SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative" + " operand should be " + expected + " but was " + actual); - actual = calculator.calculate(firstPositiveOperand, zeroOperand, subtraction); - expected = firstPositiveOperand - zeroOperand; + actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, SUBTRACTION); + expected = FIRST_POSITIVE_OPERAND - ZERO_OPERAND; assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a zero" + " operand should be " + expected + " but was " + actual); } @Test - void subtractionOperationWithMinOrMaxOperand() { - double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, subtraction); + void calculate_subtractionOperationWithMinOrMaxOperand_Ok() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, SUBTRACTION); double expected = Double.MIN_VALUE - Double.MAX_VALUE; assertEquals(expected, actual, "Test failed! Subtraction of min value and max value" + " should be " + expected + " but was " + actual); } @Test - void divisionPositiveOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, division); - double expected = firstPositiveOperand / secondPositiveOperand; + void calculate_divisionPositiveOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, DIVISION); + double expected = FIRST_POSITIVE_OPERAND / SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of two positive operands should be " + expected + " but was " + actual); } @Test - void divisionNegativeOperands() { - double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, division); - double expected = firstNegativeOperand / secondNegativeOperand; + void calculate_ivisionNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); + double expected = FIRST_NEGATIVE_OPERAND / SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of two negative operands should be " + expected + " but was " + actual); } @Test - void divisionPositiveAndNegativeOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, division); - double expected = firstPositiveOperand / secondNegativeOperand; + void calculate_divisionPositiveAndNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); + double expected = FIRST_POSITIVE_OPERAND / SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" + " operand should be " + expected + " but was " + actual); } @Test - void divisionOperationWithOneZeroOperand() { - double actual = calculator.calculate(zeroOperand, secondNegativeOperand, division); - double expected = zeroOperand / secondNegativeOperand; + void calculate_divisionOperationWithOneZeroOperand_Ok() { + double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); + double expected = ZERO_OPERAND / SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of a zero operand and a negative" + " operand should be " + expected + " but was " + actual); assertThrows(ArithmeticException.class, () -> - calculator.calculate(firstPositiveOperand, zeroOperand, division), + calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, DIVISION), "Test failed! Division by zero must throw the Arithmetic exception."); } @Test - void divisionOperationWithMinOrMaxOperand() { - double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, division); + void calculate_divisionOperationWithMinOrMaxOperand_Ok() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, DIVISION); double expected = Double.MIN_VALUE / Double.MAX_VALUE; assertEquals(expected, actual, "Test failed! Division of min value and max value" + " should be " + expected + " but was " + actual); } @Test - void multiplicationPositiveOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, - multiplication); - double expected = firstPositiveOperand * secondPositiveOperand; + void calculate_multiplicationPositiveOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, + MULTIPLICATION); + double expected = FIRST_POSITIVE_OPERAND * SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Multiplication of two positive operands" + " should be " + expected + " but was " + actual); } @Test - void multiplicationNegativeOperands() { - double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, - multiplication); - double expected = firstNegativeOperand * secondNegativeOperand; + void calculate_multiplicationNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, + MULTIPLICATION); + double expected = FIRST_NEGATIVE_OPERAND * SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Multiplication of two negative operands" + " should be " + expected + " but was " + actual); } @Test - void multiplicationPositiveAndNegativeOperands() { - double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, - multiplication); - double expected = firstPositiveOperand * secondNegativeOperand; + void calculate_multiplicationPositiveAndNegativeOperands_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, + MULTIPLICATION); + double expected = FIRST_POSITIVE_OPERAND * SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" + " a negative operand should be " + expected + " but was " + actual); } @Test - void multiplicationOperationWithOneZeroOperand() { - double actual = calculator.calculate(zeroOperand, secondNegativeOperand, multiplication); - double expected = zeroOperand * secondNegativeOperand; + void calculate_multiplicationOperationWithOneZeroOperand_Ok() { + double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, MULTIPLICATION); + double expected = ZERO_OPERAND * SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and" + " a negative operand should be " + expected + " but was " + actual); - actual = calculator.calculate(firstPositiveOperand, zeroOperand, multiplication); - expected = firstPositiveOperand * zeroOperand; + actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, MULTIPLICATION); + expected = FIRST_POSITIVE_OPERAND * ZERO_OPERAND; assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" + " a zero operand should be " + expected + " but was " + actual); } @Test - void multiplicationOperationWithMinOrMaxOperand() { - double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, multiplication); + void calculate_multiplicationOperationWithMinOrMaxOperand_Ok() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, MULTIPLICATION); double expected = Double.MIN_VALUE * Double.MAX_VALUE; assertEquals(expected, actual, "Test failed! Multiplication of min value and max value" + " should be " + expected + " but was " + actual); } @Test - void raisingToPositivePower() { - double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, - raisingToPower); - double expected = Math.pow(firstPositiveOperand, secondPositiveOperand); + void calculate_raisingToPositivePower_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, + RAISING_TO_POWER); + double expected = Math.pow(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND); assertEquals(expected, actual, "Test failed! Raising to positive power should be " + expected + " but was " + actual); } @Test - void raisingToNegativePower() { - double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, - raisingToPower); - double expected = Math.pow(firstNegativeOperand, secondNegativeOperand); + void calculate_raisingToNegativePower_Ok() { + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, + RAISING_TO_POWER); + double expected = Math.pow(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND); assertEquals(expected, actual, "Test failed! Raising to negative power should be " + expected + " but was " + actual); } @Test - void raisingToZeroPower() { - double actual = calculator.calculate(firstPositiveOperand, zeroOperand, raisingToPower); - double expected = Math.pow(firstPositiveOperand, zeroOperand); + void calculate_raisingToZeroPower_Ok() { + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, RAISING_TO_POWER); + double expected = Math.pow(FIRST_POSITIVE_OPERAND, ZERO_OPERAND); assertEquals(expected, actual, "Test failed! Raising to zero power" + " should be " + expected + " but was " + actual); } @Test - void raisingZeroToPower() { - double actual = calculator.calculate(zeroOperand, secondNegativeOperand, raisingToPower); - double expected = Math.pow(zeroOperand, secondNegativeOperand); + void calculate_raisingZeroToPower_Ok() { + double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, RAISING_TO_POWER); + double expected = Math.pow(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND); assertEquals(expected, actual, "Test failed! Raising zero to negative power " + " should be " + expected + " but was " + actual); } @Test - void calculate_illegalOperation_notOk() { + void calculate_calculate_illegalOperation_notOk() { assertThrows(IllegalArgumentException.class, () -> - calculator.calculate(firstPositiveOperand, secondPositiveOperand, - illegalOperation)); + calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, + ILLEGAL_OPERATION)); } } From d44054b9e5631eff02655b1e58faeede5e27b796 Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 14:53:29 +0300 Subject: [PATCH 3/6] Added new solution (with student requires) --- src/test/java/core/basesyntax/CalculatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index 5b9ed7af..d84bcaff 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -249,7 +249,7 @@ void calculate_raisingZeroToPower_Ok() { @Test void calculate_calculate_illegalOperation_notOk() { assertThrows(IllegalArgumentException.class, () -> - calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, - ILLEGAL_OPERATION)); + calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ILLEGAL_OPERATION), + "Test failed! Only math operations can be used for math operations."); } } From 7ae6aa7243514f8005d8f5ed21c914b1d51654c2 Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 15:00:36 +0300 Subject: [PATCH 4/6] Added new solution (with student requires) --- .../java/core/basesyntax/CalculatorTest.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index d84bcaff..51327580 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -29,7 +29,8 @@ static void beforeAll() { @Test void calculate_additionPositiveOperands_Ok() { - double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ADDITION); + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + SECOND_POSITIVE_OPERAND, ADDITION); double expected = FIRST_POSITIVE_OPERAND + SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " + expected + " but was " + actual); @@ -37,7 +38,8 @@ void calculate_additionPositiveOperands_Ok() { @Test void calculate_additionNegativeOperands_Ok() { - double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, + SECOND_NEGATIVE_OPERAND, ADDITION); double expected = FIRST_NEGATIVE_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of two negative operands should be " + expected + " but was " + actual); @@ -45,7 +47,8 @@ void calculate_additionNegativeOperands_Ok() { @Test void calculate_additionPositiveAndNegativeOperands_Ok() { - double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + SECOND_NEGATIVE_OPERAND, ADDITION); double expected = FIRST_POSITIVE_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" + " operand should be " + expected + " but was " + actual); @@ -53,12 +56,14 @@ void calculate_additionPositiveAndNegativeOperands_Ok() { @Test void calculate_additionOperationWithOneZeroOperand_Ok() { - double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); + double actual = calculator.calculate(ZERO_OPERAND, + SECOND_NEGATIVE_OPERAND, ADDITION); double expected = ZERO_OPERAND + SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative" + " operand should be " + expected + " but was " + actual); - actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, ADDITION); + actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + ZERO_OPERAND, ADDITION); expected = FIRST_POSITIVE_OPERAND + ZERO_OPERAND; assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" + " operand should be " + expected + " but was " + actual); @@ -122,7 +127,8 @@ void calculate_subtractionOperationWithMinOrMaxOperand_Ok() { @Test void calculate_divisionPositiveOperands_Ok() { - double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, DIVISION); + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + SECOND_POSITIVE_OPERAND, DIVISION); double expected = FIRST_POSITIVE_OPERAND / SECOND_POSITIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of two positive operands should be " + expected + " but was " + actual); @@ -130,7 +136,8 @@ void calculate_divisionPositiveOperands_Ok() { @Test void calculate_ivisionNegativeOperands_Ok() { - double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); + double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, + SECOND_NEGATIVE_OPERAND, DIVISION); double expected = FIRST_NEGATIVE_OPERAND / SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of two negative operands should be " + expected + " but was " + actual); @@ -138,7 +145,8 @@ void calculate_ivisionNegativeOperands_Ok() { @Test void calculate_divisionPositiveAndNegativeOperands_Ok() { - double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + SECOND_NEGATIVE_OPERAND, DIVISION); double expected = FIRST_POSITIVE_OPERAND / SECOND_NEGATIVE_OPERAND; assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" + " operand should be " + expected + " but was " + actual); @@ -232,7 +240,8 @@ void calculate_raisingToNegativePower_Ok() { @Test void calculate_raisingToZeroPower_Ok() { - double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, RAISING_TO_POWER); + double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, + ZERO_OPERAND, RAISING_TO_POWER); double expected = Math.pow(FIRST_POSITIVE_OPERAND, ZERO_OPERAND); assertEquals(expected, actual, "Test failed! Raising to zero power" + " should be " + expected + " but was " + actual); @@ -240,7 +249,8 @@ void calculate_raisingToZeroPower_Ok() { @Test void calculate_raisingZeroToPower_Ok() { - double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, RAISING_TO_POWER); + double actual = calculator.calculate(ZERO_OPERAND, + SECOND_NEGATIVE_OPERAND, RAISING_TO_POWER); double expected = Math.pow(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND); assertEquals(expected, actual, "Test failed! Raising zero to negative power " + " should be " + expected + " but was " + actual); @@ -249,7 +259,8 @@ void calculate_raisingZeroToPower_Ok() { @Test void calculate_calculate_illegalOperation_notOk() { assertThrows(IllegalArgumentException.class, () -> - calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ILLEGAL_OPERATION), + calculator.calculate(FIRST_POSITIVE_OPERAND, + SECOND_POSITIVE_OPERAND, ILLEGAL_OPERATION), "Test failed! Only math operations can be used for math operations."); } } From bf6d7d7a6d5cdb5c26187b3e4fa561e7e07b792a Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 15:50:16 +0300 Subject: [PATCH 5/6] The names of the double variables have been changed. --- src/main/java/core/basesyntax/Calculator.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index d4ddf1e9..cc7b2e81 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -2,21 +2,21 @@ public class Calculator { - public double calculate(double value1, double value2, char operation) { + public double calculate(double number1, double number2, char operation) { switch (operation) { case '+': - return value1 + value2; + return number1 + number2; case '-': - return value1 - value2; + return number1 - number2; case '*': - return value1 * value2; + return number1 * number2; case '/': - if (value2 == 0) { + if (number2 == 0) { throw new ArithmeticException("Division by zero"); } - return value1 / value2; + return number1 / number2; case '^': - return Math.pow(value1, value2); + return Math.pow(number1, number2); default: throw new IllegalArgumentException("Illegal argument operation"); } From 87656979630f4e1ec3bada930449082c021ff653 Mon Sep 17 00:00:00 2001 From: taras-oleksiuk Date: Tue, 14 Sep 2021 16:32:15 +0300 Subject: [PATCH 6/6] The names of the double variables have been changed to firstOperand and secondOperator. --- src/main/java/core/basesyntax/Calculator.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index cc7b2e81..55f52c31 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -2,21 +2,21 @@ public class Calculator { - public double calculate(double number1, double number2, char operation) { + public double calculate(double firstOperand, double secondOperator, char operation) { switch (operation) { case '+': - return number1 + number2; + return firstOperand + secondOperator; case '-': - return number1 - number2; + return firstOperand - secondOperator; case '*': - return number1 * number2; + return firstOperand * secondOperator; case '/': - if (number2 == 0) { + if (secondOperator == 0) { throw new ArithmeticException("Division by zero"); } - return number1 / number2; + return firstOperand / secondOperator; case '^': - return Math.pow(number1, number2); + return Math.pow(firstOperand, secondOperator); default: throw new IllegalArgumentException("Illegal argument operation"); }