-
Notifications
You must be signed in to change notification settings - Fork 258
implement calculator #294
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
Nazar-Hetun
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
Nazar-Hetun:Calculator
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
implement calculator #294
Changes from all commits
Commits
Show all changes
3 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,27 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| public double calculate(double firstValue, double secondValue, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return firstValue + secondValue; | ||
| case '-': | ||
| return firstValue - secondValue; | ||
| case '/': | ||
| if (secondValue == 0) { | ||
| throw new RuntimeException("Error, division by 0"); | ||
| } | ||
| return firstValue / secondValue; | ||
| case '*': | ||
| return firstValue * secondValue; | ||
| case '^': | ||
| if (firstValue < 0 && secondValue != (long) secondValue) { | ||
| throw new RuntimeException("Error"); | ||
| } | ||
| return Math.pow(firstValue, secondValue); | ||
| default: | ||
| throw new RuntimeException("Error, unknown operation type"); | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
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,236 @@ | ||
| 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; | ||
|
|
||
| public class CalculatorTest { | ||
| private static Calculator calculator; | ||
| private static final double DELTA = 0.0001; | ||
| private static final double POSITIVE_NUMBER_1 = 23.4; | ||
| private static final double POSITIVE_NUMBER_2 = 700.5; | ||
| private static final double POSITIVE_NUMBER_3 = 321.45; | ||
| private static final double POSITIVE_NUMBER_4 = 8; | ||
| private static final double POSITIVE_NUMBER_5 = 25; | ||
| private static final double POSITIVE_NUMBER_6 = 2.5; | ||
| private static final double POSITIVE_NUMBER_7 = 4.5; | ||
| private static final double NEGATIVE_NUMBER_1 = -23.4; | ||
| private static final double NEGATIVE_NUMBER_2 = -700.5; | ||
| private static final double NEGATIVE_NUMBER_3 = -8; | ||
| private static final double NEGATIVE_NUMBER_4 = -25; | ||
| private static final double NEGATIVE_NUMBER_5 = -2.5; | ||
| private static final double ZERO_NUMBER = 0; | ||
| private double actual; | ||
| private double expected; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithTwoPositiveOperands_isOk() { | ||
| actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); | ||
| expected = 723.9; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithTwoNegativeOperands_isOk() { | ||
| expected = -723.9; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithPositiveAndNegativeOperands_isOk() { | ||
| expected = 677.1; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithZeroInDifferentPlaces_isOk() { | ||
| expected = POSITIVE_NUMBER_2; | ||
| actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '+'); | ||
| assertEquals(expected, actual); | ||
| expected = POSITIVE_NUMBER_3; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionForMinAndMaxDoubleValues_isOk() { | ||
| expected = Double.MAX_VALUE; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithTwoPositiveOperands_isOk() { | ||
| actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); | ||
| expected = -677.1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithTwoNegativeOperands_isOk() { | ||
| expected = 677.1; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithPositiveAndNegativeOperands_isOk() { | ||
| expected = -723.9; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithZeroInDifferentPlaces_isOk() { | ||
| expected = NEGATIVE_NUMBER_2; | ||
| actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '-'); | ||
| assertEquals(expected, actual); | ||
| expected = POSITIVE_NUMBER_3; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionForMinAndMaxDoubleValues_isOk() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-'); | ||
| expected = Double.MAX_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithTwoPositiveOperands_isOk() { | ||
| actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); | ||
| expected = 16391.7; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithTwoNegativeOperands_isOk() { | ||
| expected = 16391.7; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithPositiveAndNegativeOperands_isOk() { | ||
| expected = -16391.7; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithZeroInDifferentPlaces_isOk() { | ||
| expected = ZERO_NUMBER; | ||
| actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '*'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationForMinAndMaxDoubleValues_isOk() { | ||
| expected = ZERO_NUMBER; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithTwoPositiveOperands_isOk() { | ||
| expected = 0.32; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_4, POSITIVE_NUMBER_5, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithTwoNegativeOperands_isOk() { | ||
| expected = 0.32; | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_3, NEGATIVE_NUMBER_4, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithPositiveAndNegativeOperands_isOk() { | ||
| expected = -0.32; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_4, NEGATIVE_NUMBER_4, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithZeroInDifferentPlaces() { | ||
| expected = ZERO_NUMBER; | ||
| actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_5, '/'); | ||
| assertEquals(expected, actual); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(POSITIVE_NUMBER_4, ZERO_NUMBER, '/')); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingForMinAndMaxDoubleValues() { | ||
| expected = 1; | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveOrNegativeValueToThePositivePower() { | ||
| expected = 61.763235; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_6, POSITIVE_NUMBER_7, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(NEGATIVE_NUMBER_5, POSITIVE_NUMBER_7, '^')); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveOrNegativeValueToTheNegativePower() { | ||
| expected = 0.02327; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_7, NEGATIVE_NUMBER_5, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(NEGATIVE_NUMBER_5, NEGATIVE_NUMBER_5, '^')); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveOrNegativeValueToTheZeroPower() { | ||
| expected = 1; | ||
| actual = calculator.calculate(POSITIVE_NUMBER_7, ZERO_NUMBER, '^'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(NEGATIVE_NUMBER_5, ZERO_NUMBER, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroToPower() { | ||
| expected = ZERO_NUMBER; | ||
| actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_7, '^'); | ||
| assertEquals(expected, actual); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calculator.calculate(ZERO_NUMBER, NEGATIVE_NUMBER_5, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperations_ExceptionOk() { | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '=')); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '!')); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '$')); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '%')); | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '(')); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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.
what about negative power?