-
Notifications
You must be signed in to change notification settings - Fork 257
implement calculator #294
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?
implement calculator #294
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,27 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| public double calculate(double a, double b, char c) { | ||
| switch (c) { | ||
| case '+': | ||
| return a + b; | ||
| case '-': | ||
| return a - b; | ||
| case '/': | ||
| if (b == 0) { | ||
| throw new RuntimeException("Error, division by 0"); | ||
| } | ||
| return a / b; | ||
| case '*': | ||
| return a * b; | ||
| case '^': | ||
| if (a < 0 && b != (long) b) { | ||
| throw new RuntimeException("Error"); | ||
| } | ||
|
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. There is no need to throw Exception. We can raise any number to any power from a mathematical point of view. |
||
| return Math.pow(a, b); | ||
| default: | ||
| throw new RuntimeException("Error, unknown operation type"); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| 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; | ||
|
|
||
| public class CalculatorTest { | ||
| private static Calculator calculator; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| /* | ||
| Tests for additions. | ||
| */ | ||
|
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. Don't use comments. Your code should be understandable without them |
||
|
|
||
| @Test | ||
| void additionWithTwoPositiveOperands_isOk() { | ||
|
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. what method do we test? |
||
| double a = 23.4; | ||
| double b = 700.5; | ||
|
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. frequently used variables can be translated into constants. |
||
| double actual = calculator.calculate(a, b, '+'); | ||
| double expected = 723.9; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithTwoNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = -700.5; | ||
|
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. frequently used variables can be translated into constants. |
||
| double expected = -723.9; | ||
| double actual = calculator.calculate(a, b, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithPositiveAndNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = 700.5; | ||
| double expected = 677.1; | ||
| double actual = calculator.calculate(a, b, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithZeroInDifferentPlaces_isOk() { | ||
| double a = 0; | ||
| double b = 700.5; | ||
| double expected = 700.5; | ||
| double actual = calculator.calculate(a, b, '+'); | ||
| assertEquals(expected, actual); | ||
| a = 321.45; | ||
| b = 0; | ||
| expected = 321.45; | ||
| actual = calculator.calculate(a, b, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionForMinAndMaxDoubleValues_isOk() { | ||
| double a = Double.MAX_VALUE; | ||
| double b = Double.MIN_VALUE; | ||
| double delta = 0.0001; | ||
|
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. the delta variable must be declared constant. Check Common mistakes |
||
| double actual = calculator.calculate(a, b, '+'); | ||
| assertEquals(a, actual, delta); | ||
| } | ||
|
|
||
| /* | ||
| Tests for subtractions. | ||
| */ | ||
|
|
||
| @Test | ||
| void subtractionWithTwoPositiveOperands_isOk() { | ||
| double a = 23.4; | ||
| double b = 700.5; | ||
| double actual = calculator.calculate(a, b, '-'); | ||
| double expected = -677.1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithTwoNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = -700.5; | ||
| double expected = 677.1; | ||
| double actual = calculator.calculate(a, b, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithPositiveAndNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = 700.5; | ||
| double expected = -723.9; | ||
| double actual = calculator.calculate(a, b, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithZeroInDifferentPlaces_isOk() { | ||
| double a = 0; | ||
| double b = 700.5; | ||
| double expected = -700.5; | ||
| double actual = calculator.calculate(a, b, '-'); | ||
| assertEquals(expected, actual); | ||
| a = 321.45; | ||
| b = 0; | ||
| expected = 321.45; | ||
| actual = calculator.calculate(a, b, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionForMinAndMaxDoubleValues_isOk() { | ||
| double a = Double.MAX_VALUE; | ||
| double b = Double.MIN_VALUE; | ||
| double actual = calculator.calculate(a, b, '-'); | ||
| double expected = Double.MAX_VALUE; | ||
| double delta = 0.0001; | ||
| assertEquals(expected, actual, delta); | ||
| } | ||
|
|
||
| /* | ||
| Tests for multiplication. | ||
| */ | ||
|
|
||
| @Test | ||
| void multiplicationWithTwoPositiveOperands_isOk() { | ||
| double a = 23.4; | ||
| double b = 700.5; | ||
| double actual = calculator.calculate(a, b, '*'); | ||
| double expected = 16391.7; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithTwoNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = -700.5; | ||
| double expected = 16391.7; | ||
| double actual = calculator.calculate(a, b, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithPositiveAndNegativeOperands_isOk() { | ||
| double a = -23.4; | ||
| double b = 700.5; | ||
| double expected = -16391.7; | ||
| double actual = calculator.calculate(a, b, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithZeroInDifferentPlaces_isOk() { | ||
| double a = 0; | ||
| double b = 700.5; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(a, b, '*'); | ||
| assertEquals(expected, actual); | ||
| a = 321.45; | ||
| b = 0; | ||
| actual = calculator.calculate(a, b, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationForMinAndMaxDoubleValues_isOk() { | ||
| double a = Double.MAX_VALUE; | ||
| double b = Double.MIN_VALUE; | ||
| double actual = calculator.calculate(a, b, '*'); | ||
| double delta = 0.0001; | ||
| assertEquals(0, actual, delta); | ||
| } | ||
|
|
||
| /* | ||
| Tests for division. | ||
| */ | ||
|
|
||
| @Test | ||
| void divisionWithTwoPositiveOperands_isOk() { | ||
| double a = 8; | ||
| double b = 25; | ||
| double expected = 0.32; | ||
| double actual = calculator.calculate(a, b, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithTwoNegativeOperands_isOk() { | ||
| double a = -8; | ||
| double b = -25; | ||
| double expected = 0.32; | ||
| double actual = calculator.calculate(a, b, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithPositiveAndNegativeOperands_isOk() { | ||
| double a = 8; | ||
| double b = -25; | ||
| double expected = -0.32; | ||
| double actual = calculator.calculate(a, b, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithZeroInDifferentPlaces() { | ||
| double a = 0; | ||
| double b = 25; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(a, b, '/'); | ||
| assertEquals(expected, actual); | ||
| double anotherA = 8; | ||
| double anotherB = 0; | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(anotherA, anotherB, '/')); | ||
| } | ||
|
|
||
| /* | ||
| Tests for ^ operation. | ||
| */ | ||
|
|
||
| @Test | ||
| void raisingForMinAndMaxDoubleValues() { | ||
| double a = Double.MAX_VALUE; | ||
| double b = Double.MIN_VALUE; | ||
| double expected = 1; | ||
| double delta = 0.0001; | ||
| double actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual, delta); | ||
| a = Double.MIN_VALUE; | ||
| b = Double.MAX_VALUE; | ||
| actual = calculator.calculate(a, b, '^'); | ||
| expected = 0; | ||
| assertEquals(expected, actual, delta); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveOrNegativeValueToThePositivePower() { | ||
| final double b = 2.5; | ||
| double a = 4.5; | ||
| double expected = 42.956737; | ||
| double delta = 0.0001; | ||
| double actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual, delta); | ||
| double negativeA = -4.5; | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveOrNegativeValueToTheNegativePower() { | ||
| double a = 4.5; | ||
| double b = -2.2; | ||
| double expected = 0.03655; | ||
| double delta = 0.0001; | ||
| double actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual, delta); | ||
| double negativeA = -4.5; | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveOrNegativeValueToTheZeroPower() { | ||
| double a = 4.5; | ||
| double b = 0; | ||
| double expected = 1; | ||
| double actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual); | ||
| a = -4.5; | ||
| expected = 1; | ||
| actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToPower() { | ||
| double a = 0; | ||
| double b = 4.5; | ||
| double expected = 0; | ||
| double actual = calculator.calculate(a, b, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| /* | ||
| Tests for illegal operation. | ||
| */ | ||
|
|
||
| @Test | ||
| void illegalOperations_ExceptionOk() { | ||
| double finalA = 24; | ||
| double finalB = -8; | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '=')); | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '!')); | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '$')); | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '%')); | ||
| assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '(')); | ||
| } | ||
| } | ||
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.
use descriptive parameter and variable names.