-
Notifications
You must be signed in to change notification settings - Fork 257
added solution #293
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
Open
kvoytikh
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
kvoytikh:kvoytikh-done-task
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
added solution #293
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package core.basesyntax; | ||
|
|
||
| class Calculator { | ||
| public double calculate(double firstOperand, double secondOperand, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return firstOperand + secondOperand; | ||
| case '-': | ||
| return firstOperand - secondOperand; | ||
| case '/': | ||
| if (secondOperand == 0) { | ||
| throw new ArithmeticException("Division by zero is not possible!"); | ||
| } | ||
| return firstOperand / secondOperand; | ||
| case '*': | ||
| return firstOperand * secondOperand; | ||
| case '^': | ||
| return Math.pow(firstOperand, secondOperand); | ||
| default: | ||
| throw new IllegalArgumentException("Given invalid operation"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| 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 final double DELTA = 0.0001; | ||
| private static Calculator calculator; | ||
| private double expected; | ||
| private double actual; | ||
|
|
||
| @BeforeAll | ||
| static void setUp() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionTwoPositiveOperands_Ok() { | ||
| expected = 30.0; | ||
| actual = calculator.calculate(20.0, 10.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionTwoNegativeOperands_Ok() { | ||
| expected = -30.0; | ||
| actual = calculator.calculate(-20.0, -10.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionPositiveAndNegativeOperands_Ok() { | ||
| expected = 20.0; | ||
| actual = calculator.calculate(30.0, -10.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionFirstZeroOperand_Ok() { | ||
| expected = 10.0; | ||
| actual = calculator.calculate(0, 10.0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionSecondZeroOperand_Ok() { | ||
| expected = 40.0; | ||
| actual = calculator.calculate(40.0, 0, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionMaxDoubleOperands_Ok() { | ||
| expected = Double.MAX_VALUE + Double.MIN_VALUE; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionTwoPositiveOperands_Ok() { | ||
| expected = 30.0; | ||
| actual = calculator.calculate(40.0, 10.0, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionTwoNegativeOperands_Ok() { | ||
| expected = -30.0; | ||
| actual = calculator.calculate(-40.0, -10.0, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionPositiveAndNegativeOperands_Ok() { | ||
| expected = 50.0; | ||
| actual = calculator.calculate(40.0, -10.0, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionFirstZeroOperand_Ok() { | ||
| expected = -10.0; | ||
| actual = calculator.calculate(0, 10.0, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionSecondZeroOperand_Ok() { | ||
| expected = 40.0; | ||
| actual = calculator.calculate(40.0, 0, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMaxDoubleOperands_Ok() { | ||
| expected = Double.MAX_VALUE - Double.MIN_VALUE; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationTwoPositiveOperands_Ok() { | ||
| expected = 400.0; | ||
| actual = calculator.calculate(40.0, 10.0, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationTwoNegativeOperands_Ok() { | ||
| expected = 400.0; | ||
| actual = calculator.calculate(-40.0, -10.0, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationPositiveAndNegativeOperands_Ok() { | ||
| expected = -400.0; | ||
| actual = calculator.calculate(40.0, -10.0, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationFirstZeroOperand_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculate(0, 10.0, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationSecondZeroOperand_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculate(40.0, 0, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMaxDoubleOperands_Ok() { | ||
| expected = Double.MAX_VALUE * Double.MIN_VALUE; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionTwoPositiveOperands_Ok() { | ||
| expected = 4.0; | ||
| actual = calculator.calculate(40.0, 10.0, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionTwoNegativeOperands_Ok() { | ||
| expected = 4.0; | ||
| actual = calculator.calculate(-40.0, -10.0, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionPositiveAndNegativeOperands_Ok() { | ||
| expected = -4.0; | ||
| actual = calculator.calculate(40.0, -10.0, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionFirstZeroOperand_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculate(0, 10.0, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionSecondZeroOperand_Ok() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(10, 0, '/'); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMaxDoubleOperands_Ok() { | ||
| expected = Double.MAX_VALUE / Double.MIN_VALUE; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '/'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToPositivePower_Ok() { | ||
| expected = 27.0; | ||
| actual = calculator.calculate(3.0, 3.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToPositivePower_Ok() { | ||
| expected = 9.0; | ||
| actual = calculator.calculate(-3.0, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToNegativePower_Ok() { | ||
| expected = 0.1; | ||
| actual = calculator.calculate(10.0, -1.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToNegativePower_Ok() { | ||
| expected = -1; | ||
| actual = calculator.calculate(-1, -1, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroValueToPower_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculate(0.0, 1, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToZeroPower_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculate(10.0, 0.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToZeroPower_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculate(-10.0, 0.0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingFirstIsZeroToNegativePower() { | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calculator.calculate(0, -2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_Ok() { | ||
| char illegalOperation = 'a'; | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(100.0, 100.0, illegalOperation); | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
добавь еще 1 метод firstOperand = 0, secondOperand = -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.
done