-
Notifications
You must be signed in to change notification settings - Fork 257
implemented task lambda calc #304
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
alex-hazniuk
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
alex-hazniuk:hw-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
2 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 Calculation { | ||
| double calculate(double number1, double number2, 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,21 @@ | ||||||||
| package core.basesyntax; | ||||||||
|
|
||||||||
| public class Calculator implements Calculation { | ||||||||
| @Override | ||||||||
| public double calculate(double number1, double number2, char operation) { | ||||||||
| switch (operation) { | ||||||||
| case '+': | ||||||||
| return number1 + number2; | ||||||||
| case '-': | ||||||||
| return number1 - number2; | ||||||||
| case '*': | ||||||||
| return number1 * number2; | ||||||||
| case '/': | ||||||||
| return number1 / number2; | ||||||||
| case '^': | ||||||||
| return Math.pow(number1, number2); | ||||||||
| default: | ||||||||
| throw new IllegalOperationException("Illegal operator"); | ||||||||
|
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.
Suggested change
лучше делать сообщение ошибок как можно более информативными |
||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
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,212 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CalculatorTest { | ||
| private static final double DELTA = 0.1; | ||
| private static Calculation count; | ||
|
|
||
| @BeforeAll | ||
| public static void beforeAll() { | ||
| count = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_positiveOperands() { | ||
| double actual = count.calculate(6, 7, '+'); | ||
| assertEquals(13, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_negativeOperands() { | ||
| double actual = count.calculate(-9, -11, '+'); | ||
| assertEquals(-20, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_positiveNegativeOperands() { | ||
| double actual = count.calculate(10, -20, '+'); | ||
| assertEquals(-10, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_zeroOperands() { | ||
| double actual = count.calculate(0, 0, '+'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_operandsMinValues() { | ||
| double actual = count.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '+'); | ||
| assertEquals(1.0E-323, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_addition_operandsMaxValues() { | ||
| double actual = count.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '+'); | ||
| assertTrue(Double.isInfinite(actual)); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_positiveOperands() { | ||
| double actual = count.calculate(6, 7, '-'); | ||
| assertEquals(-1, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_negativeOperands() { | ||
| double actual = count.calculate(-9, -11, '-'); | ||
| assertEquals(2, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_positiveNegativeOperands() { | ||
| double actual = count.calculate(10, -20, '-'); | ||
| assertEquals(30, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_zeroOperands() { | ||
| double actual = count.calculate(0, 0, '-'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_operandsMinValues() { | ||
| double actual = count.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '-'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_subtraction_operandsMaxValues() { | ||
| double actual = count.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '-'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_positiveOperands() { | ||
| double actual = count.calculate(6, 7, '*'); | ||
| assertEquals(42, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_negativeOperands() { | ||
| double actual = count.calculate(-9, -11, '*'); | ||
| assertEquals(99, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_positiveNegativeOperands() { | ||
| double actual = count.calculate(10, -20, '*'); | ||
| assertEquals(-200, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_zeroOperands() { | ||
| double actual = count.calculate(0, 2, '*'); | ||
| assertEquals(0, actual); | ||
| actual = count.calculate(2, 0, '*'); | ||
| assertEquals(0, actual); | ||
| actual = count.calculate(0, 0, '*'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_operandsMinValues() { | ||
| double actual = count.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '*'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_multiplication_operandsMaxValues() { | ||
| double actual = count.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '*'); | ||
| assertTrue(Double.isInfinite(actual)); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_positiveOperands() { | ||
| double actual = count.calculate(1.4, 1.2, '/'); | ||
| assertEquals(1.2, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_negativeOperands() { | ||
| double actual = count.calculate(-10, -2, '/'); | ||
| assertEquals(5, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_positiveNegativeOperands() { | ||
| double actual = count.calculate(10, -2, '/'); | ||
| assertEquals(-5, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_zeroDividendOperand() { | ||
| double actual = count.calculate(0, 5, '/'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_zeroDividerOperand() { | ||
| double actual = count.calculate(5, 0, '/'); | ||
| assertTrue(Double.isInfinite(actual)); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_operandsMinValues() { | ||
| double actual = count.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '/'); | ||
| assertEquals(1, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void calculate_division_operandsMaxValues() { | ||
| double actual = count.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '/'); | ||
| assertEquals(1, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raising_positiveNegativeValue_positivePower() { | ||
| double actual = count.calculate(2, 3, '^'); | ||
| assertEquals(8, actual); | ||
| actual = count.calculate(-2, 3, '^'); | ||
| assertEquals(-8, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raising_positiveNegativeValue_negativePower() { | ||
| double actual = count.calculate(2, -2, '^'); | ||
| assertEquals(0.25, actual); | ||
| actual = count.calculate(-2, -2, '^'); | ||
| assertEquals(0.25, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raising_positiveNegativeValue_zeroPower() { | ||
| double actual = count.calculate(2, 0, '^'); | ||
| assertEquals(1, actual); | ||
| actual = count.calculate(-2, 0, '^'); | ||
| assertEquals(1, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raising_zeroValue_positiveNegativePower() { | ||
| double actual = count.calculate(0, -2, '^'); | ||
| assertTrue(Double.isInfinite(actual)); | ||
| actual = count.calculate(0, 2, '^'); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_isNotOk() { | ||
| char invalidOperation = '%'; | ||
| assertThrows(IllegalOperationException.class, () -> { | ||
| count.calculate(0, 2, invalidOperation); | ||
| }); | ||
| } | ||
| } |
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.
плохая практика использовать числа в названии переменных и аргументов