-
Notifications
You must be signed in to change notification settings - Fork 257
Added new solution #292
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
taras-oleksiuk
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
taras-oleksiuk:hw-1
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 new solution #292
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9b1db7
Added new solution
taras-oleksiuk 4185064
Added new solution (with student requires)
taras-oleksiuk d44054b
Added new solution (with student requires)
taras-oleksiuk 7ae6aa7
Added new solution (with student requires)
taras-oleksiuk bf6d7d7
The names of the double variables have been changed.
taras-oleksiuk 8765697
The names of the double variables have been changed to firstOperand a…
taras-oleksiuk 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,21 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| public double calculate(double value1, double value2, char operation) { | ||
| switch (operation) { | ||
| case '+': return value1 + value2; | ||
| case '-': return value1 - value2; | ||
| case '*': return value1 * value2; | ||
|
taras-oleksiuk marked this conversation as resolved.
Outdated
|
||
| case '/': | ||
| if (value2 == 0) { | ||
| throw new ArithmeticException("Division by zero"); | ||
| } | ||
| return value1 / value2; | ||
| case '^': | ||
| return Math.pow(value1, value2); | ||
| default: | ||
| throw new IllegalArgumentException("Illegal argument 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,255 @@ | ||
| 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 { | ||
|
|
||
|
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. redundant empty line |
||
| private static Calculator calculator; | ||
| private final double firstPositiveOperand = 11.13; | ||
| private final double secondPositiveOperand = 12.14; | ||
| private final double firstNegativeOperand = -21.23; | ||
| private final double secondNegativeOperand = -22.24; | ||
| private final int zeroOperand = 0; | ||
|
taras-oleksiuk marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final char addition = '+'; | ||
| private final char subtraction = '-'; | ||
| private final char division = '/'; | ||
| private final char multiplication = '*'; | ||
| private final char raisingToPower = '^'; | ||
| private final char illegalOperation = '$'; | ||
|
taras-oleksiuk marked this conversation as resolved.
Outdated
|
||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void additionPositiveOperands() { | ||
|
taras-oleksiuk marked this conversation as resolved.
Outdated
|
||
| double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, addition); | ||
| double expected = firstPositiveOperand + secondPositiveOperand; | ||
| assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionNegativeOperands() { | ||
| double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, addition); | ||
| double expected = firstNegativeOperand + secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Addition of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionPositiveAndNegativeOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, addition); | ||
| double expected = firstPositiveOperand + secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionOperationWithOneZeroOperand() { | ||
| double actual = calculator.calculate(zeroOperand, secondNegativeOperand, addition); | ||
| double expected = zeroOperand + secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(firstPositiveOperand, zeroOperand, addition); | ||
| expected = firstPositiveOperand + zeroOperand; | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionOperationWithMinOrMaxOperand() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, addition); | ||
| double expected = Double.MIN_VALUE + Double.MAX_VALUE; | ||
| assertEquals(expected, actual, "Test failed! Addition of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionPositiveOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, | ||
| subtraction); | ||
| double expected = firstPositiveOperand - secondPositiveOperand; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionNegativeOperands() { | ||
| double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, | ||
| subtraction); | ||
| double expected = firstNegativeOperand - secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionPositiveAndNegativeOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, | ||
| subtraction); | ||
| double expected = firstPositiveOperand - secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a " | ||
| + "negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionOperationWithOneZeroOperand() { | ||
| double actual = calculator.calculate(zeroOperand, secondNegativeOperand, subtraction); | ||
| double expected = zeroOperand - secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(firstPositiveOperand, zeroOperand, subtraction); | ||
| expected = firstPositiveOperand - zeroOperand; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a zero" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionOperationWithMinOrMaxOperand() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, subtraction); | ||
| double expected = Double.MIN_VALUE - Double.MAX_VALUE; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionPositiveOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, division); | ||
| double expected = firstPositiveOperand / secondPositiveOperand; | ||
| assertEquals(expected, actual, "Test failed! Division of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionNegativeOperands() { | ||
| double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, division); | ||
| double expected = firstNegativeOperand / secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Division of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionPositiveAndNegativeOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, division); | ||
| double expected = firstPositiveOperand / secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionOperationWithOneZeroOperand() { | ||
| double actual = calculator.calculate(zeroOperand, secondNegativeOperand, division); | ||
| double expected = zeroOperand / secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Division of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
|
|
||
| assertThrows(ArithmeticException.class, () -> | ||
| calculator.calculate(firstPositiveOperand, zeroOperand, division), | ||
| "Test failed! Division by zero must throw the Arithmetic exception."); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionOperationWithMinOrMaxOperand() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, division); | ||
| double expected = Double.MIN_VALUE / Double.MAX_VALUE; | ||
| assertEquals(expected, actual, "Test failed! Division of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationPositiveOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, | ||
| multiplication); | ||
| double expected = firstPositiveOperand * secondPositiveOperand; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationNegativeOperands() { | ||
| double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, | ||
| multiplication); | ||
| double expected = firstNegativeOperand * secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationPositiveAndNegativeOperands() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, | ||
| multiplication); | ||
| double expected = firstPositiveOperand * secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationOperationWithOneZeroOperand() { | ||
| double actual = calculator.calculate(zeroOperand, secondNegativeOperand, multiplication); | ||
| double expected = zeroOperand * secondNegativeOperand; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(firstPositiveOperand, zeroOperand, multiplication); | ||
| expected = firstPositiveOperand * zeroOperand; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a zero operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationOperationWithMinOrMaxOperand() { | ||
| double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, multiplication); | ||
| double expected = Double.MIN_VALUE * Double.MAX_VALUE; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingToPositivePower() { | ||
| double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, | ||
| raisingToPower); | ||
| double expected = Math.pow(firstPositiveOperand, secondPositiveOperand); | ||
| assertEquals(expected, actual, "Test failed! Raising to positive power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingToNegativePower() { | ||
| double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, | ||
| raisingToPower); | ||
| double expected = Math.pow(firstNegativeOperand, secondNegativeOperand); | ||
| assertEquals(expected, actual, "Test failed! Raising to negative power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingToZeroPower() { | ||
| double actual = calculator.calculate(firstPositiveOperand, zeroOperand, raisingToPower); | ||
| double expected = Math.pow(firstPositiveOperand, zeroOperand); | ||
| assertEquals(expected, actual, "Test failed! Raising to zero power" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToPower() { | ||
| double actual = calculator.calculate(zeroOperand, secondNegativeOperand, raisingToPower); | ||
| double expected = Math.pow(zeroOperand, secondNegativeOperand); | ||
| assertEquals(expected, actual, "Test failed! Raising zero to negative power " | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| calculator.calculate(firstPositiveOperand, secondPositiveOperand, | ||
| illegalOperation)); | ||
| } | ||
| } | ||
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.