-
Notifications
You must be signed in to change notification settings - Fork 257
Implemented calculator #284
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
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,23 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
| public double calculate(double number1, double number2, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return number1 + number2; | ||
| case '-': | ||
| return number1 - number2; | ||
| case '*': | ||
| return number1 * number2; | ||
| case '^': | ||
| return Math.pow(number1, number2); | ||
| case '/': | ||
| if (number2 == 0) { | ||
| throw new IllegalArgumentException("Can't divide 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. maybe ArithmeticException would be better here? |
||
| } | ||
| return number1 / number2; | ||
| default: | ||
| throw new IllegalStateException("Invalid operation"); | ||
|
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. |
||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| 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 Calculator calculator = new Calculator(); | ||
|
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. lets initialize this variable in @BeforeAll method
Author
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. Why do we need it? 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. Обычно в тестах объекты сложные и требуют определенной доинициализации (что делается в методе @BeforeAll). В данном случае объект простой, и в принципе можно было оставить инициализацию в момент объявления, но мы хотим что б вы знали про метод @BeforeAll и умели им пользоваться. |
||
| private double expected; | ||
| private double actual; | ||
| private double number1; | ||
| private double number2; | ||
|
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. numbers in variables naming |
||
|
|
||
| @Test | ||
| void addictionTwoPositive() { | ||
| number1 = 7; | ||
| number2 = 14; | ||
| expected = 21; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionTwoNegative() { | ||
| number1 = -7; | ||
|
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. you use the same numbers everywhere, so you can simplify the code, make them constants
Author
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. Часто бачив, як в наших тестах до завдань константи до чисел не записувались, тому вирішив прописати всі числа напряму 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. не обязательно выносить это в константы, но в тестах лучше использовать разные данные, это повышает вероятность найти ошибку |
||
| number2 = -14; | ||
| expected = -21; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionPositiveAndNegative() { | ||
|
MaliukDaria marked this conversation as resolved.
|
||
| number1 = 7; | ||
| number2 = -14; | ||
| expected = -7; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
|
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. I think it worth to add here the check with first negative operant and second positive |
||
| } | ||
|
|
||
| @Test | ||
| void addictionZero_1() { | ||
|
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. It is also bad practice to use numbers in method names. |
||
| number1 = 0; | ||
| number2 = -14; | ||
| expected = -14; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionZero_2() { | ||
| number1 = -14; | ||
| number2 = 0; | ||
| expected = -14; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
Comment on lines
+42
to
+56
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. we can combine this tests, they test the same logic |
||
|
|
||
| @Test | ||
| void addictionMaxValue() { | ||
| number1 = Double.MAX_VALUE; | ||
| number2 = -7; | ||
| expected = Double.MAX_VALUE; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionMinValue() { | ||
| number1 = Double.MIN_VALUE; | ||
| number2 = 0; | ||
| expected = Double.MIN_VALUE; | ||
| actual = calculator.calculate(number1, number2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionTwoPositive() { | ||
| number1 = 7; | ||
| number2 = 14; | ||
| expected = -7; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionTwoNegative() { | ||
| number1 = -7; | ||
| number2 = -14; | ||
| expected = 7; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionPositiveAndNegative() { | ||
| number1 = 7; | ||
| number2 = -14; | ||
| expected = 21; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionZero_1() { | ||
| number1 = 0; | ||
| number2 = -14; | ||
| expected = 14; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionZero_2() { | ||
| number1 = -14; | ||
| number2 = 0; | ||
| expected = -14; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionMaxValue() { | ||
| number1 = Double.MAX_VALUE; | ||
| number2 = -7; | ||
| expected = Double.MAX_VALUE; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionMinValue() { | ||
| number1 = Double.MIN_VALUE; | ||
| number2 = 3; | ||
| expected = -3; | ||
| actual = calculator.calculate(number1, number2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoPositive() { | ||
| number1 = 7; | ||
| number2 = 14; | ||
| expected = 98; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoNegative() { | ||
| number1 = -7; | ||
| number2 = -14; | ||
| expected = 98; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationPositiveAndNegative() { | ||
| number1 = 7; | ||
| number2 = -14; | ||
| expected = -98; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationZero_1() { | ||
| number1 = 0; | ||
| number2 = 14; | ||
| expected = 0; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationZero_2() { | ||
| number1 = 14; | ||
| number2 = 0; | ||
| expected = 0; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationMaxValue() { | ||
| number1 = Double.MAX_VALUE; | ||
| number2 = 7; | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationMinValue() { | ||
| number1 = Double.MIN_VALUE; | ||
| number2 = 3; | ||
| expected = 1.5E-323; | ||
| actual = calculator.calculate(number1, number2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionTwoPositive() { | ||
| number1 = 7; | ||
| number2 = 14; | ||
| expected = 0.5; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionTwoNegative() { | ||
| number1 = -7; | ||
| number2 = -14; | ||
| expected = 0.5; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionPositiveAndNegative() { | ||
| number1 = 7; | ||
| number2 = -14; | ||
| expected = -0.5; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionZero_1() { | ||
| number1 = 0; | ||
| number2 = 14; | ||
| expected = 0; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionZero_2() { | ||
| number1 = 14; | ||
| number2 = 0; | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(number1, number2, '/'); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionMaxValue() { | ||
| number1 = Double.MAX_VALUE; | ||
| number2 = 7; | ||
| expected = 2.5681330498033083E307; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionMinValue() { | ||
| number1 = Double.MIN_VALUE; | ||
| number2 = 3; | ||
| expected = 0; | ||
| actual = calculator.calculate(number1, number2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToPower() { | ||
| number1 = 7; | ||
| number2 = 3; | ||
| expected = 343; | ||
| actual = calculator.calculate(number1, number2, '^'); | ||
| assertEquals(expected, actual); | ||
| number1 = -7; | ||
| number2 = 3; | ||
| expected = -343; | ||
| actual = calculator.calculate(number1, number2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToNegativePower() { | ||
| number1 = 7; | ||
| number2 = -1; | ||
| expected = 0.14285714285714285; | ||
| actual = calculator.calculate(number1, number2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToZeroPower() { | ||
| number1 = 7; | ||
| number2 = 0; | ||
| expected = 1; | ||
| actual = calculator.calculate(number1, number2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void checkOperation() { | ||
| number1 = 7; | ||
| number2 = 4; | ||
| assertThrows(IllegalStateException.class, () -> { | ||
| calculator.calculate(number1, number2, '!'); | ||
| }); | ||
| assertThrows(IllegalStateException.class, () -> { | ||
| calculator.calculate(number1, number2, '3'); | ||
| }); | ||
| } | ||
| } | ||
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.
It's bad practice to use numbers in variable names