-
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 2 commits
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,24 @@ | ||
| 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; | ||
| 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 static final double FIRST_POSITIVE_OPERAND = 11.13; | ||
| private static final double SECOND_POSITIVE_OPERAND = 12.14; | ||
| private static final double FIRST_NEGATIVE_OPERAND = -21.23; | ||
| private static final double SECOND_NEGATIVE_OPERAND = -22.24; | ||
| private static final double ZERO_OPERAND = 0.0; | ||
|
|
||
|
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 final char ADDITION = '+'; | ||
| private static final char SUBTRACTION = '-'; | ||
| private static final char DIVISION = '/'; | ||
| private static final char MULTIPLICATION = '*'; | ||
| private static final char RAISING_TO_POWER = '^'; | ||
| private static final char ILLEGAL_OPERATION = '$'; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionPositiveOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, ADDITION); | ||
| double expected = FIRST_POSITIVE_OPERAND + SECOND_POSITIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); | ||
| double expected = FIRST_NEGATIVE_OPERAND + SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Addition of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); | ||
| double expected = FIRST_POSITIVE_OPERAND + SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionOperationWithOneZeroOperand_Ok() { | ||
| double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, ADDITION); | ||
| double expected = ZERO_OPERAND + SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, ADDITION); | ||
| expected = FIRST_POSITIVE_OPERAND + ZERO_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionOperationWithMinOrMaxOperand_Ok() { | ||
| 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 calculate_subtractionPositiveOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, | ||
| SUBTRACTION); | ||
| double expected = FIRST_POSITIVE_OPERAND - SECOND_POSITIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, | ||
| SUBTRACTION); | ||
| double expected = FIRST_NEGATIVE_OPERAND - SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, | ||
| SUBTRACTION); | ||
| double expected = FIRST_POSITIVE_OPERAND - SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a " | ||
| + "negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionOperationWithOneZeroOperand_Ok() { | ||
| double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, SUBTRACTION); | ||
| double expected = ZERO_OPERAND - SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, SUBTRACTION); | ||
| expected = FIRST_POSITIVE_OPERAND - ZERO_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a zero" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionOperationWithMinOrMaxOperand_Ok() { | ||
| 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 calculate_divisionPositiveOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, DIVISION); | ||
| double expected = FIRST_POSITIVE_OPERAND / SECOND_POSITIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Division of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_ivisionNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); | ||
| double expected = FIRST_NEGATIVE_OPERAND / SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Division of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); | ||
| double expected = FIRST_POSITIVE_OPERAND / SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionOperationWithOneZeroOperand_Ok() { | ||
| double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, DIVISION); | ||
| double expected = ZERO_OPERAND / SECOND_NEGATIVE_OPERAND; | ||
| 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(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, DIVISION), | ||
| "Test failed! Division by zero must throw the Arithmetic exception."); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionOperationWithMinOrMaxOperand_Ok() { | ||
| 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 calculate_multiplicationPositiveOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, | ||
| MULTIPLICATION); | ||
| double expected = FIRST_POSITIVE_OPERAND * SECOND_POSITIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, | ||
| MULTIPLICATION); | ||
| double expected = FIRST_NEGATIVE_OPERAND * SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_NEGATIVE_OPERAND, | ||
| MULTIPLICATION); | ||
| double expected = FIRST_POSITIVE_OPERAND * SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationOperationWithOneZeroOperand_Ok() { | ||
| double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, MULTIPLICATION); | ||
| double expected = ZERO_OPERAND * SECOND_NEGATIVE_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
|
|
||
| actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, MULTIPLICATION); | ||
| expected = FIRST_POSITIVE_OPERAND * ZERO_OPERAND; | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a zero operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationOperationWithMinOrMaxOperand_Ok() { | ||
| 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 calculate_raisingToPositivePower_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, | ||
| RAISING_TO_POWER); | ||
| double expected = Math.pow(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND); | ||
| assertEquals(expected, actual, "Test failed! Raising to positive power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingToNegativePower_Ok() { | ||
| double actual = calculator.calculate(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND, | ||
| RAISING_TO_POWER); | ||
| double expected = Math.pow(FIRST_NEGATIVE_OPERAND, SECOND_NEGATIVE_OPERAND); | ||
| assertEquals(expected, actual, "Test failed! Raising to negative power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingToZeroPower_Ok() { | ||
| double actual = calculator.calculate(FIRST_POSITIVE_OPERAND, ZERO_OPERAND, RAISING_TO_POWER); | ||
| double expected = Math.pow(FIRST_POSITIVE_OPERAND, ZERO_OPERAND); | ||
| assertEquals(expected, actual, "Test failed! Raising to zero power" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroToPower_Ok() { | ||
| double actual = calculator.calculate(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND, RAISING_TO_POWER); | ||
| double expected = Math.pow(ZERO_OPERAND, SECOND_NEGATIVE_OPERAND); | ||
| assertEquals(expected, actual, "Test failed! Raising zero to negative power " | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| calculator.calculate(FIRST_POSITIVE_OPERAND, SECOND_POSITIVE_OPERAND, | ||
| ILLEGAL_OPERATION)); | ||
|
taras-oleksiuk marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
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.