-
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
Open
Radikhovskiy
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
Radikhovskiy:develop
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
Changes from all commits
Commits
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; | ||
|
|
||
| public class Calculator { | ||
| public double calculate(double first, double second, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return first + second; | ||
| case '-': | ||
| return first - second; | ||
| case '*': | ||
| return first * second; | ||
| case '^': | ||
| return Math.pow(first, second); | ||
| case '/': | ||
| if (second == 0) { | ||
| throw new ArithmeticException("Can't divide by zero"); | ||
| } | ||
| return first / second; | ||
| default: | ||
| throw new IllegalArgumentException("Invalid operation"); | ||
| } | ||
| } | ||
| } |
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,296 @@ | ||
| 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; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionTwoPositive() { | ||
| double first = 7; | ||
| double second = 14; | ||
| double expected = 21; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionTwoNegative() { | ||
| double first = -38; | ||
| double second = -17; | ||
| double expected = -55; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionPositiveAndNegative() { | ||
| double first = 40; | ||
| double second = -20; | ||
| double expected = 20; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionZero() { | ||
| double first = 0; | ||
| double second = 93; | ||
| double expected = 93; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| first = 36; | ||
| second = 0; | ||
| expected = 36; | ||
| actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionMaxValue() { | ||
| double first = Double.MAX_VALUE; | ||
| double second = -7; | ||
| double expected = Double.MAX_VALUE; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addictionMinValue() { | ||
| double first = Double.MIN_VALUE; | ||
| double second = 0; | ||
| double expected = Double.MIN_VALUE; | ||
| double actual = calculator.calculate(first, second, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionTwoPositive() { | ||
| double first = 15; | ||
| double second = 10; | ||
| double expected = 5; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionTwoNegative() { | ||
| double first = -3; | ||
| double second = -4; | ||
| double expected = 1; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionPositiveAndNegative() { | ||
| double first = 34; | ||
| double second = -14; | ||
| double expected = 48; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionZero() { | ||
| double first = 0; | ||
| double second = -17; | ||
| double expected = 17; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| first = -11; | ||
| second = 0; | ||
| expected = -11; | ||
| actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionMaxValue() { | ||
| double first = Double.MAX_VALUE; | ||
| double second = -4; | ||
| double expected = Double.MAX_VALUE; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionMinValue() { | ||
| double first = Double.MIN_VALUE; | ||
| double second = 3; | ||
| double expected = -3; | ||
| double actual = calculator.calculate(first, second, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoPositive() { | ||
| double first = 6; | ||
| double second = 4; | ||
| double expected = 24; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoNegative() { | ||
| double first = -7; | ||
| double second = -14; | ||
| double expected = 98; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationPositiveAndNegative() { | ||
| double first = 7; | ||
| double second = -21; | ||
| double expected = -147; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationZero() { | ||
| double first = 0; | ||
| double second = 346; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| first = 52; | ||
| second = 0; | ||
| expected = 0; | ||
| actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationMaxValue() { | ||
| double first = Double.MAX_VALUE; | ||
| double second = 32; | ||
| double expected = Double.POSITIVE_INFINITY; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationMinValue() { | ||
| double first = Double.MIN_VALUE; | ||
| double second = 3; | ||
| double expected = 1.5E-323; | ||
| double actual = calculator.calculate(first, second, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionTwoPositive() { | ||
| double first = 6; | ||
| double second = 2; | ||
| double expected = 3; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionTwoNegative() { | ||
| double first = -10; | ||
| double second = -5; | ||
| double expected = 2; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionPositiveAndNegative() { | ||
| double first = 20; | ||
| double second = -5; | ||
| double expected = -4; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionZero() { | ||
| double first = 0; | ||
| double second = 300; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| double firstL = 35437; | ||
| double secondL = 0; | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(firstL, secondL, '/'); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionMaxValue() { | ||
| double first = Double.MAX_VALUE; | ||
| double second = 7; | ||
| double expected = 2.5681330498033083E307; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionMinValue() { | ||
| double first = Double.MIN_VALUE; | ||
| double second = 3; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(first, second, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToPower() { | ||
| double first = 7; | ||
| double second = 3; | ||
| double expected = 343; | ||
| double actual = calculator.calculate(first, second, '^'); | ||
| assertEquals(expected, actual); | ||
| first = -7; | ||
| second = 3; | ||
| expected = -343; | ||
| actual = calculator.calculate(first, second, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToNegativePower() { | ||
| double first = 7; | ||
| double second = -1; | ||
| double expected = 0.14285714285714285; | ||
| double actual = calculator.calculate(first, second, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseToZeroPower() { | ||
| double first = 4363; | ||
| double second = 0; | ||
| double expected = 1; | ||
| double actual = calculator.calculate(first, second, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void checkOperation() { | ||
| double first = 7; | ||
| double second = 4; | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(first, second, '!'); | ||
| }); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(first, second, '3'); | ||
| }); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.