-
Notifications
You must be signed in to change notification settings - Fork 257
jv-lambda-calc task completed #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
94dcd83
ccb7025
b051fa0
31bf680
b6574b8
f7c8672
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public interface Calculate { | ||
| double calculate(double a, double b, char c); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator implements Calculate { | ||
|
|
||
| @Override | ||
| public double calculate(double firstParameter, double secondParameter, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return firstParameter + secondParameter; | ||
| case '-': | ||
| return firstParameter - secondParameter; | ||
| case '*': | ||
| return firstParameter * secondParameter; | ||
| case '^': | ||
| return Math.pow(firstParameter, secondParameter); | ||
| case '/': | ||
| if (secondParameter == 0) { | ||
| throw new IllegalArgumentException("Division by zero"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw new ArithmeticException("Division by zero is not possible!"); |
||
| } | ||
| return firstParameter / secondParameter; | ||
| default: | ||
| throw new IllegalArgumentException("Invalid operation"); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CalculatorTest { | ||
| private static final double DELTA = 0.0001; | ||
| private Calculate calculator = new Calculator(); | ||
|
|
||
| @Test | ||
| void additionWithTwoPositiveOperands_Ok() { | ||
| double expected = 100.0; | ||
| double actual = calculator.calculate(90.0, 10.0, '+'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. создай 1 раз expected and actual и используй их в каждом методе |
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithTwoNegativeOperands_Ok() { | ||
| double expected = - 150.0; | ||
| double actual = calculator.calculate(-100.0, -50.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithPositiveAndNegativeOperands_Ok() { | ||
| double expected = - 150.0; | ||
| double actual = calculator.calculate(-200.0, 50.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithFirstOperandIsZero_Ok() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. измени все названия методов по примеру calculate_additionPositiveAndNegativeOperand_ok |
||
| double expected = 50.0; | ||
| double actual = calculator.calculate(0.0, 50.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithSecondOperandIsZero_Ok() { | ||
| double expected = 60.0; | ||
| double actual = calculator.calculate(60.0, 0.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithMinAndMaxDoubleValues_Ok() { | ||
| double expected = Double.MAX_VALUE + Double.MIN_VALUE; | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithMinAndMaxDoubleValues_Ok() { | ||
| double expected = Double.MAX_VALUE - Double.MIN_VALUE; | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithMinAndMaxDoubleValues_Ok() { | ||
| double expected = Double.MAX_VALUE * Double.MIN_VALUE; | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithMinAndMaxDoubleValues_Ok() { | ||
| double expected = Double.MAX_VALUE / Double.MIN_VALUE; | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '/'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionByZero_NotOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(2.0, 0.0, '/'); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToThePositivePower_Ok() { | ||
| double expected = 64.0; | ||
| double actual = calculator.calculate(8.0, 2.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToThePositivePower_Ok() { | ||
| double expected = 64.0; | ||
| double actual = calculator.calculate(-8.0, 2.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToTheNegativePower_Ok() { | ||
| double expected = 0.015625; | ||
| double actual = calculator.calculate(8.0, -2.0, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToTheNegativePower_Ok() { | ||
| double expected = 0.015625; | ||
| double actual = calculator.calculate(-8.0, -2.0, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToZeroPower_Ok() { | ||
| double expected = 1.0; | ||
| double actual = calculator.calculate(8.0, 0.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToZeroPower_Ok() { | ||
| double expected = 1.0; | ||
| double actual = calculator.calculate(-8.0, 0.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToPower_Ok() { | ||
| double expected = 0.0; | ||
| double actual = calculator.calculate(0.0, 10.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void illegalOperation_NotOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(2.0, 3.0, '#'); | ||
| }); | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а что если первый операнд 0, а второй -2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
добавлена проверка Double.POSITIVE_INFINITY