-
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
base: master
Are you sure you want to change the base?
Done solution #296
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||||||
|
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.
Suggested change
|
||||||
| 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class CalculatorTest { | ||
| private static final double DELTA = 0.000001; | ||
| private static Calculator calc; | ||
|
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. Calculator calculator |
||
| private double expected; | ||
| private double actual; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| calc = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveAddition() { | ||
|
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. Wrong test methods names, read README.md more carefully. For this task use such convention: ; For example, if we are testing the method calculate with an illegal character passed as an operation the test method name should be calculate_illegalOperation_notOk. notOk is because the test expects the calculate method to throw an exception. 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. And the same to everything. |
||
| expected = 8; | ||
| actual = calc.calculator(1, 7, '+'); | ||
|
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. Create private static variable and use them instead of raw numbers. |
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeAddition() { | ||
| expected = -8; | ||
| actual = calc.calculator(-1, -7, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeAddition() { | ||
| expected = 6; | ||
| actual = calc.calculator(-1, 7, '+'); | ||
| assertEquals(expected, actual); | ||
| expected = -6; | ||
| actual = calc.calculator(1, -7, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroAddition() { | ||
| expected = 1; | ||
| actual = calc.calculator(1, 0, '+'); | ||
| assertEquals(expected, actual); | ||
| actual = calc.calculator(0, 1, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesAddition() { | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| expected = 0; | ||
| actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveSubtraction() { | ||
| expected = -6; | ||
| actual = calc.calculator(1, 7, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeSubtraction() { | ||
| expected = 6; | ||
| actual = calc.calculator(-1, -7, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeSubtraction() { | ||
| expected = 8; | ||
| actual = calc.calculator(1, -7, '-'); | ||
| assertEquals(expected, actual); | ||
| expected = -8; | ||
| actual = calc.calculator(-1, 7, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroSubtraction() { | ||
| expected = 1; | ||
| actual = calc.calculator(1, 0, '-'); | ||
| assertEquals(expected, actual); | ||
| expected = -1; | ||
| actual = calc.calculator(0, 1, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesSubtraction() { | ||
| expected = 0; | ||
| actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| expected = 0; | ||
| actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveMultiplication() { | ||
| expected = 7; | ||
| actual = calc.calculator(1, 7, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeMultiplication() { | ||
| expected = 7; | ||
| actual = calc.calculator(-1, -7, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeMultiplication() { | ||
| expected = -7; | ||
| actual = calc.calculator(-1, 7, '*'); | ||
| assertEquals(expected, actual); | ||
| expected = -7; | ||
| actual = calc.calculator(1, -7, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void differentPlacesZeroMultiplication() { | ||
| expected = 0; | ||
| actual = calc.calculator(1, 0, '*'); | ||
| assertEquals(expected, actual); | ||
| expected = 0; | ||
| actual = calc.calculator(0, 7, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValuesMultiplication() { | ||
| expected = Double.POSITIVE_INFINITY; | ||
| actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '*'); | ||
| assertEquals(expected, actual); | ||
| expected = 0; | ||
| actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveDivision() { | ||
| expected = 2; | ||
| actual = calc.calculator(4,2, '/'); | ||
|
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. calc.calculator(4, 2, '/'); |
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativeDivision() { | ||
| expected = 2; | ||
| actual = calc.calculator(-4,-2, '/'); | ||
|
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. 😈 |
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void positiveNegativeDivision() { | ||
| expected = -2; | ||
| actual = calc.calculator(-4, 2, '/'); | ||
| assertEquals(expected, actual); | ||
| expected = -2; | ||
| actual = calc.calculator(4, -2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void firstPlacesZeroDivision() { | ||
| expected = 0; | ||
| actual = calc.calculator(0, 2, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void secondPlacesZeroDivision() { | ||
| assertThrows(ArithmeticException.class, | ||
| () -> calc.calculator(2, 0, '/')); | ||
| } | ||
|
|
||
| @Test | ||
| public void maxMinValueDivision() { | ||
| expected = 1; | ||
| actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '/'); | ||
| assertEquals(expected, actual); | ||
| expected = 1; | ||
| actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingPositiveToPositiveOrNegative() { | ||
|
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. raisingToPower, but I doubt it critical |
||
| expected = 64; | ||
| actual = calc.calculator(4, 3, '^'); | ||
| assertEquals(expected, actual); | ||
| expected = 0.0625; | ||
| actual = calc.calculator(4, -2, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingNegativeToPositiveOrNegative() { | ||
| expected = 16; | ||
| actual = calc.calculator(-4, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| expected = 0.0625; | ||
| actual = calc.calculator(-4, -2, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingValueToZero() { | ||
| expected = 1; | ||
| actual = calc.calculator(2, 0, '^'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| public void raisingZeroToPower() { | ||
|
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. I was commented that it must be tested with positive and negative. |
||
| expected = 0; | ||
| actual = calc.calculator(0, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void illegalOperation() { | ||
| assertThrows(IllegalArgumentException.class, () -> calc.calculator(1, 2, '&')); | ||
| } | ||
| } | ||
This file was deleted.
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.
I dob't remember adding this dependecy, and it still worked ok with org.junit.jupiter.api.Assertions