-
Notifications
You must be signed in to change notification settings - Fork 257
My Calculator #283
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
DenysST
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
DenysST:HW
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
My Calculator #283
Changes from 2 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,5 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public interface Calculator { | ||
| double calculate(double firstValue, double secondValue, char 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,30 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class MyCalculator implements Calculator { | ||
| public static final char ADDITION = '+'; | ||
| public static final char SUBTRACTION = '-'; | ||
| public static final char DIVISION = '/'; | ||
| public static final char MULTIPLICATION = '*'; | ||
| public static final char RISING_TO_POWER = '^'; | ||
|
|
||
| @Override | ||
| public double calculate(double firstValue, double secondValue, char operation) { | ||
| switch (operation) { | ||
| case ADDITION : | ||
| return firstValue + secondValue; | ||
| case SUBTRACTION : | ||
| return firstValue - secondValue; | ||
| case DIVISION : | ||
| if (secondValue == 0) { | ||
| throw new ArithmeticException("Second value can't be 0!"); | ||
| } | ||
| return firstValue / secondValue; | ||
| case MULTIPLICATION : | ||
| return firstValue * secondValue; | ||
| case RISING_TO_POWER : | ||
| return Math.pow(firstValue, secondValue); | ||
| default: | ||
| throw new UnknownOperationException("Unknown 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,7 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class UnknownOperationException extends RuntimeException { | ||
| public UnknownOperationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
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,260 @@ | ||
| 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 MyCalculatorTest { | ||
| public static final char ADDITION = '+'; | ||
| public static final char SUBTRACTION = '-'; | ||
| public static final char DIVISION = '/'; | ||
| public static final char MULTIPLICATION = '*'; | ||
| public static final char RISING = '^'; | ||
| private static final double DELTA = 0.00001; | ||
| private static Calculator calculator; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new MyCalculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void additionTwoPositiveOperands_OK() { | ||
| double actual = calculator.calculate(10, 5, ADDITION); | ||
| double expected = 15; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionTwoNegativeOperands_OK() { | ||
| double actual = calculator.calculate(-10, -5, ADDITION); | ||
| double expected = -15; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionNegativeAndPositiveOperands_OK() { | ||
| double actual = calculator.calculate(-10,5, ADDITION); | ||
| double expected = -5; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithZero_OK() { | ||
| double actual = calculator.calculate(0, 10, ADDITION); | ||
| double expected = 10; | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(10, 0, ADDITION); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithMaxValues_OK() { | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, ADDITION); | ||
| double expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithMinValue_OK() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, ADDITION); | ||
| double expected = 0; | ||
| assertEquals(expected,actual,DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithTwoPositiveOperands_OK() { | ||
| double actual = calculator.calculate(10, 5, SUBTRACTION); | ||
| double expected = 5; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithTwoNegativeOperands_OK() { | ||
| double actual = calculator.calculate(-10, -5, SUBTRACTION); | ||
| double expected = -5; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithNegativeAndPositiveOperands_OK() { | ||
| double actual = calculator.calculate(-5, 10, SUBTRACTION); | ||
| double expected = -15; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithZeroOperands_OK() { | ||
| double actual = calculator.calculate(10, 0, SUBTRACTION); | ||
| double expected = 10; | ||
| assertEquals(expected, actual); | ||
| expected = -10; | ||
| actual = calculator.calculate(0, 10, SUBTRACTION); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithMaxValues_OK() { | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, SUBTRACTION); | ||
| double expected = 0; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithMinValues_OK() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, SUBTRACTION); | ||
| double expected = 0; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithTwoPositiveOperands_OK() { | ||
| double actual = calculator.calculate(10, 5, DIVISION); | ||
| double expected = 2; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithTwoNegativeOperands_OK() { | ||
| double actual = calculator.calculate(-10, -5, DIVISION); | ||
| double expected = 2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithNegativeAndPositiveOperands_OK() { | ||
| double actual = calculator.calculate(-10, 2, DIVISION); | ||
| double expected = -5; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithFirstZeroOperands_OK() { | ||
| double actual = calculator.calculate(0, 10, DIVISION); | ||
| double expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionOnZero_NotOK() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(10, 0, DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithMaxValues_OK() { | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, DIVISION); | ||
| double expected = 1; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithMinValues_OK() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, DIVISION); | ||
| double expected = 1; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoPositiveOperands_OK() { | ||
| double actual = calculator.calculate(10, 5, MULTIPLICATION); | ||
| double expected = 50; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationTwoNegativeOperands_OK() { | ||
| double actual = calculator.calculate(-10, -5, MULTIPLICATION); | ||
| double expected = 50; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationNegativeAndPositiveOperands_OK() { | ||
| double actual = calculator.calculate(-10,5, MULTIPLICATION); | ||
| double expected = -50; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithZero_OK() { | ||
| double actual = calculator.calculate(0, 10, MULTIPLICATION); | ||
| double expected = 0; | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(10, 0, MULTIPLICATION); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithMaxValues_OK() { | ||
| double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, MULTIPLICATION); | ||
| double expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithMinValue_OK() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, MULTIPLICATION); | ||
| double expected = 0; | ||
| assertEquals(expected,actual,DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveToPositivePower_OK() { | ||
| double expected = 32; | ||
| double actual = calculator.calculate(2, 5, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveToNegativePower_OK() { | ||
| double expected = 0.125; | ||
| double actual = calculator.calculate(2, -3, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeToNegativePower_OK() { | ||
| double expected = -0.125; | ||
| double actual = calculator.calculate(-2, -3, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeToPositivePower_OK() { | ||
| double expected = -27; | ||
| double actual = calculator.calculate(-3, 3, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToZeroPower_OK() { | ||
| double expected = 1; | ||
| double actual = calculator.calculate(5, 0, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToZeroPower_OK() { | ||
| double expected = 1; | ||
| double actual = calculator.calculate(-5, 0, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToPower_OK() { | ||
|
Contributor
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. what about |
||
| double expected = 0; | ||
| double actual = calculator.calculate(0, 5, RISING); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void unknownOperation_OK() { | ||
| assertThrows(UnknownOperationException.class, () -> { | ||
| calculator.calculate(10, 10, '.'); | ||
| }); | ||
| } | ||
| } | ||
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 method do we test?