-
Notifications
You must be signed in to change notification settings - Fork 257
Created Calculator class and calculate method in SimpleCalculator interface #290
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
dandalb
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
dandalb:hw_jv-lambda-calc
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95b0612
Created Calculator class and calculate method in SimpleCalculator int…
dandalb 1ca2e9a
fixed name methods
dandalb 4e1fb11
similar tests are combined
dandalb 43d3f7e
BeforeEach -> BeforeAll
dandalb 4af6231
Added all mentor recommendations
dandalb 65561bc
Fixed calculate_exponentiationValueToZero_Ok() method
dandalb 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,25 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator implements SimpleCalculator { | ||
|
|
||
| @Override | ||
| public double calculate(double firstValue, double secondValue, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return firstValue + secondValue; | ||
| case '-': | ||
| return firstValue - secondValue; | ||
| case '*': | ||
| return firstValue * secondValue; | ||
| case '/': | ||
| if (secondValue == 0) { | ||
| throw new RuntimeException("Can`t division by zero"); | ||
| } | ||
| return firstValue / secondValue; | ||
| case '^': | ||
| return Math.pow(firstValue, secondValue); | ||
| default: | ||
| throw new IllegalOperationException("Illegal 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,7 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class IllegalOperationException extends RuntimeException { | ||
| public IllegalOperationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
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 SimpleCalculator { | ||
| double calculate(double firstValue, double secondValue, char 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,281 @@ | ||
| 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 FIRST_POSITIVE_OPERAND = 1.5; | ||
| private static final double SECOND_POSITIVE_OPERAND = 8.50; | ||
| private static final double FIRST_NEGATIVE_OPERAND = -100.450; | ||
| private static final double SECOND_NEGATIVE_OPERAND = -5.0; | ||
| private static final double DELTA = 0.0001; | ||
| private static final byte ZERO = 0; | ||
| private static final char ADDITION = '+'; | ||
| private static final char SUBTRACTION = '-'; | ||
| private static final char MULTIPLICATION = '*'; | ||
| private static final char DIVISION = '/'; | ||
| private static final char EXPONENTIATION = '^'; | ||
| private static SimpleCalculator calculator; | ||
| private double expected; | ||
| private double actual; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionPositiveOperands_Ok() { | ||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ADDITION); | ||
| expected = 10; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionNegativeOperands_Ok() { | ||
| actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); | ||
| expected = -105.450; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithFirstNegativeOperand_Ok() { | ||
| actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, FIRST_POSITIVE_OPERAND, ADDITION); | ||
| expected = -98.95; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, ADDITION); | ||
| expected = Double.MIN_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionMaxValuesOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, ADDITION); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionMaxAndMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, ADDITION); | ||
| expected = Double.MAX_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionPositiveOperands_Ok() { | ||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, SUBTRACTION); | ||
| expected = -7.0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionNegativeOperands_Ok() { | ||
| actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, SUBTRACTION); | ||
| expected = -95.45; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithFirstNegativeOperand_Ok() { | ||
| actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, FIRST_POSITIVE_OPERAND, SUBTRACTION); | ||
| expected = -101.95; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithSecondNegativeOperand_Ok() { | ||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, FIRST_NEGATIVE_OPERAND, SUBTRACTION); | ||
| expected = 101.95; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, SUBTRACTION); | ||
| expected = Double.MIN_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMaxOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, SUBTRACTION); | ||
| expected = ZERO; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMinAndMaxOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, SUBTRACTION); | ||
| expected = -Double.MAX_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationPositiveOperands_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, MULTIPLICATION); | ||
| expected = 12.75; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationNegativeOperands_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, MULTIPLICATION); | ||
| expected = 502.25; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithFirstNegativeOperand_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, FIRST_POSITIVE_OPERAND, MULTIPLICATION); | ||
| expected = -150.675; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, MULTIPLICATION); | ||
| expected = Double.MIN_VALUE; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMaxValuesOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, MULTIPLICATION); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMaxAndMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, MULTIPLICATION); | ||
| expected = 8.881784197001251E-16; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionPositiveOperands_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, DIVISION); | ||
| expected = 0.17647058823529413; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionNegativeOperands_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); | ||
| expected = 20.09; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithFirstNegativeOperand_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, FIRST_POSITIVE_OPERAND, DIVISION); | ||
| expected = -66.96666666666667; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithSecondNegativeOperand_Ok() { | ||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, FIRST_NEGATIVE_OPERAND, DIVISION); | ||
| expected = -0.014932802389248382; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMinOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, DIVISION); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMaxOperands_Ok() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, DIVISION); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMinAndMaxOperands_Ok() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, DIVISION); | ||
| expected = ZERO; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionByZero_notOk() { | ||
| assertThrows(RuntimeException.class, () -> { | ||
| calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO, DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_exponentiationToPositiveValue_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, EXPONENTIATION); | ||
| expected = 31.388871489063344; | ||
| assertEquals(expected, actual); | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, SECOND_POSITIVE_OPERAND, EXPONENTIATION); | ||
| expected = Double.NaN; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_exponentiationToNegativeValue_Ok() { | ||
| actual = calculator | ||
| .calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, EXPONENTIATION); | ||
| expected = 0.13168724279835392; | ||
| assertEquals(expected, actual); | ||
| actual = calculator | ||
| .calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, EXPONENTIATION); | ||
| expected = -9.77800589098601E-11; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_exponentiationValueToZero_Ok() { | ||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO, EXPONENTIATION); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, ZERO, EXPONENTIATION); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_exponentiationZero_Ok() { | ||
| actual = calculator.calculate(ZERO, FIRST_POSITIVE_OPERAND, EXPONENTIATION); | ||
| expected = ZERO; | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(ZERO, FIRST_NEGATIVE_OPERAND, EXPONENTIATION); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalOperationException.class, () -> { | ||
| calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, '%'); | ||
| }); | ||
| assertThrows(IllegalOperationException.class, () -> { | ||
| calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, '#'); | ||
| }); | ||
| assertThrows(IllegalOperationException.class, () -> { | ||
| calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, '<'); | ||
| }); | ||
| } | ||
| } | ||
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
zeroToNegativePower?