-
Notifications
You must be signed in to change notification settings - Fork 257
Done solution #296
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
Andruwa808
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
Andruwa808:hm-9
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
Done solution #296
Changes from 2 commits
Commits
Show all changes
4 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,24 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
| public double calculator(double firstNumber, double secondNumber, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return firstNumber + secondNumber; | ||
| case '-': | ||
| return firstNumber - secondNumber; | ||
| case '*': | ||
| return firstNumber * secondNumber; | ||
| case '/': | ||
| if (secondNumber == 0) { | ||
| throw new ArithmeticException("You can't divide by 0!"); | ||
| } | ||
| return firstNumber / secondNumber; | ||
| case '^': | ||
| return Math.pow(firstNumber, secondNumber); | ||
| default: | ||
| throw new IllegalArgumentException("This operation " + operation | ||
| + " doesn't exist"); | ||
| } | ||
| } | ||
| } | ||
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,230 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class CalculatorTest { | ||
| private static final double DELTA = 0.000001; | ||
| private static Calculator calculator = new Calculator(); | ||
| private double expected; | ||
| private double actual; | ||
|
|
||
| @Test | ||
| public void positiveAddition_Ok() { | ||
| expected = 8; | ||
| actual = calculator.calculator(1,7,'+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeAddition_Ok() { | ||
| expected = -8; | ||
| actual = calculator.calculator(-1,-7,'+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeAddition_Ok() { | ||
| expected = 6; | ||
| actual = calculator.calculator(-1,7,'+'); | ||
| assertEquals(expected, actual); | ||
| expected = -6; | ||
| actual = calculator.calculator(1,-7,'+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroAddition_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculator(1,0,'+'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculator(0,1,'+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesAddition_Ok() { | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calculator.calculator(Double.MAX_VALUE, Double.MAX_VALUE,'+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| expected = 0; | ||
| actual = calculator.calculator(Double.MIN_VALUE, Double.MIN_VALUE,'+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveSubtraction_Ok() { | ||
| expected = -6; | ||
| actual = calculator.calculator(1,7,'-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeSubtraction_Ok() { | ||
| expected = 6; | ||
| actual = calculator.calculator(-1,-7,'-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeSubtraction_Ok() { | ||
| expected = 8; | ||
| actual = calculator.calculator(1,-7,'-'); | ||
| assertEquals(expected, actual); | ||
| expected = -8; | ||
| actual = calculator.calculator(-1,7,'-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroSubtraction_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculator(1,0,'-'); | ||
| assertEquals(expected, actual); | ||
| expected = -1; | ||
| actual = calculator.calculator(0,1,'-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesSubtraction_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculator(Double.MAX_VALUE, Double.MAX_VALUE,'-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| expected = 0; | ||
| actual = calculator.calculator(Double.MIN_VALUE, Double.MIN_VALUE,'-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveMultiplication_Ok() { | ||
| expected = 7; | ||
| actual = calculator.calculator(1,7,'*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeMultiplication_Ok() { | ||
| expected = 7; | ||
| actual = calculator.calculator(-1,-7,'*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeMultiplication_Ok() { | ||
| expected = -7; | ||
| actual = calculator.calculator(-1,7,'*'); | ||
| assertEquals(expected, actual); | ||
| expected = -7; | ||
| actual = calculator.calculator(1,-7,'*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroMultiplication_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculator(1,0,'*'); | ||
| assertEquals(expected, actual); | ||
| expected = 0; | ||
| actual = calculator.calculator(0,7,'*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesMultiplication_Ok() { | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calculator.calculator(Double.MAX_VALUE, Double.MAX_VALUE,'*'); | ||
| assertEquals(expected, actual); | ||
| expected = 0; | ||
| actual = calculator.calculator(Double.MIN_VALUE, Double.MIN_VALUE,'*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveDivision_Ok() { | ||
| expected = 2; | ||
| actual = calculator.calculator(4,2,'/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeDivision_Ok() { | ||
| expected = 2; | ||
| actual = calculator.calculator(-4,-2,'/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeDivision_Ok() { | ||
| expected = -2; | ||
| actual = calculator.calculator(-4,2,'/'); | ||
| assertEquals(expected, actual); | ||
| expected = -2; | ||
| actual = calculator.calculator(4,-2,'/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void firstPlacesZeroDivision_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculator(0,2,'/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void secondPlacesZeroDivision_NotOk() { | ||
| assertThrows(ArithmeticException.class, | ||
| () -> calculator.calculator(2,0,'/')); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValueDivision_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculator(Double.MAX_VALUE, Double.MAX_VALUE,'/'); | ||
| assertEquals(expected, actual); | ||
| expected = 1; | ||
| actual = calculator.calculator(Double.MIN_VALUE, Double.MIN_VALUE,'/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingPositiveToPositiveOrNegative_Ok() { | ||
| expected = 64; | ||
| actual = calculator.calculator(4,3,'^'); | ||
| assertEquals(expected, actual); | ||
| expected = 0.0625; | ||
| actual = calculator.calculator(4,-2,'^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingNegativeToPositiveOrNegative_Ok() { | ||
| expected = 16; | ||
| actual = calculator.calculator(-4,2,'^'); | ||
| assertEquals(expected, actual); | ||
| expected = 0.0625; | ||
| actual = calculator.calculator(-4,-2,'^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingValueToZero_Ok() { | ||
| expected = 1; | ||
| actual = calculator.calculator(2,0,'^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingZeroToNegativePower_Ok() { | ||
| expected = 0; | ||
| actual = calculator.calculator(0,2,'^'); | ||
|
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. is it negative? |
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculator.calculator(1,2,'&')); | ||
| } | ||
| } | ||
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.