-
Notifications
You must be signed in to change notification settings - Fork 257
Created a calculator and wrote tests for it #291
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?
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,53 @@ | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class Calculate { | ||
| public static final char PLUS = '+'; | ||
| public static final char MINUS = '-'; | ||
| public static final char DIVISION = '/'; | ||
| public static final char MULTIPLICATION = '*'; | ||
| public static final char EXPONENTIATION = '^'; | ||
|
|
||
| public double calculate(double firstNumber, double secondNumber, char operation) { | ||
| switch (operation) { | ||
| case PLUS: | ||
| return getTheAmountResult(firstNumber, secondNumber); | ||
| case MINUS: | ||
| return getTheSubtractionResult(firstNumber, secondNumber); | ||
| case DIVISION: | ||
| return getTheDivisionResult(firstNumber, secondNumber); | ||
| case MULTIPLICATION: | ||
| return getTheMultiplicationResult(firstNumber, secondNumber); | ||
| case EXPONENTIATION: | ||
| return getTheExponentiationResult(firstNumber, secondNumber); | ||
| default: | ||
| throw new NoSuchElementException("Incorrect operation"); | ||
|
|
||
|
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. We don't need whitespace here |
||
| } | ||
| } | ||
|
|
||
| private double getTheAmountResult(double firstNumber, double secondNumber) { | ||
| return firstNumber + secondNumber; | ||
| } | ||
|
|
||
| private double getTheSubtractionResult(double firstNumber, double secondNumber) { | ||
| return firstNumber - secondNumber; | ||
| } | ||
|
|
||
| private double getTheDivisionResult(double firstNumber, double secondNumber) { | ||
| if (secondNumber == 0) { | ||
| throw new ArithmeticException("We can't divide by zero"); | ||
| } | ||
| return firstNumber / secondNumber; | ||
| } | ||
|
|
||
| private double getTheMultiplicationResult(double firstNumber, double secondNumber) { | ||
| return firstNumber * secondNumber; | ||
| } | ||
|
|
||
| private double getTheExponentiationResult(double firstNumber, double secondNumber) { | ||
| if (firstNumber == 0 && secondNumber < 0) { | ||
| throw new ArithmeticException("We can't divide by zero"); | ||
| } | ||
| return Math.pow(firstNumber, secondNumber); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.NoSuchElementException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CalculateTest { | ||
|
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 about |
||
| private static final Calculate calculate = new Calculate(); | ||
|
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. Let's rename this constant, because when we write calculate.calculate it's confusing. And also constants should be with only uppercase chars and _. |
||
|
|
||
| @Test | ||
| void additionWithTwoPositiveOperands_Ok() { | ||
|
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. Let's name our methods based on the pattern |
||
| double actual = calculate.calculate(6, 8, Calculate.PLUS); | ||
|
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. Let's create fields answer and actual in class and in tests we only initialize them |
||
| double answer = 14; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithTwoNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, -8, Calculate.PLUS); | ||
| double answer = -14; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, 8, Calculate.PLUS); | ||
| double answer = 2; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithZeroInDifferentPlaces_Ok() { | ||
| double actual = calculate.calculate(0, 8, Calculate.PLUS); | ||
| double answer = 8; | ||
| assertEquals(answer,actual); | ||
|
|
||
|
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 don't think that it is good to use whitespaces between tests in one method. Maybe is not a mistake, and that is more readable, better to ask a mentor. 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. Agree, lets remove redundant empty lines |
||
| actual = calculate.calculate(6, 0, Calculate.PLUS); | ||
| answer = 6; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(0, 0, Calculate.PLUS); | ||
| answer = 0; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithTwoPositiveOperands_Ok() { | ||
| double actual = calculate.calculate(6, 8, Calculate.MINUS); | ||
| double answer = -2; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithTwoNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, -8, Calculate.MINUS); | ||
| double answer = 2; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, 8, Calculate.MINUS); | ||
| double answer = -14; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithZeroInDifferentPlaces_Ok() { | ||
| double actual = calculate.calculate(0, 8, Calculate.MINUS); | ||
| double answer = -8; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(6, 0, Calculate.MINUS); | ||
| answer = 6; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(0, 0, Calculate.MINUS); | ||
| answer = 0; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithTwoPositiveOperands_Ok() { | ||
| double actual = calculate.calculate(6, 2, Calculate.DIVISION); | ||
| double answer = 3; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithTwoNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, -2, Calculate.DIVISION); | ||
| double answer = 3; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, 2, Calculate.DIVISION); | ||
| double answer = -3; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithZeroInStart_Ok() { | ||
| double actual = calculate.calculate(0, 8, Calculate.DIVISION); | ||
| double answer = 0; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithZeroInEnd_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculate.calculate(6, 0, Calculate.DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void divisionWithZeroInStartAndEnd_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculate.calculate(0, 0, Calculate.DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithTwoPositiveOperands_Ok() { | ||
| double actual = calculate.calculate(6, 8, Calculate.MULTIPLICATION); | ||
| double answer = 48; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithTwoNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, -8, Calculate.MULTIPLICATION); | ||
| double answer = 48; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithPositiveAndNegativeOperands_Ok() { | ||
| double actual = calculate.calculate(-6, 8, Calculate.MULTIPLICATION); | ||
| double answer = -48; | ||
| assertEquals(answer, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithZeroInDifferentPlaces_Ok() { | ||
| double actual = calculate.calculate(0, 8, Calculate.MULTIPLICATION); | ||
| double answer = 0; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(6, 0, Calculate.MULTIPLICATION); | ||
| answer = 0; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(0, 0, Calculate.MULTIPLICATION); | ||
| answer = 0; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToThePositivePower_Ok() { | ||
| double actual = calculate.calculate(5, 2, Calculate.EXPONENTIATION); | ||
| double answer = 25; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToThePositivePower_Ok() { | ||
| double actual = calculate.calculate(-5, 2, Calculate.EXPONENTIATION); | ||
| double answer = 25; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToTheNegativePower_Ok() { | ||
| double actual = calculate.calculate(10, -3, Calculate.EXPONENTIATION); | ||
| double answer = 0.001; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToTheNegativePower_Ok() { | ||
| double actual = calculate.calculate(-10, -3, Calculate.EXPONENTIATION); | ||
| double answer = -0.001; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingPositiveValueToZeroPower_Ok() { | ||
| double actual = calculate.calculate(5, 0, Calculate.EXPONENTIATION); | ||
| double answer = 1; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeValueToZeroPower_Ok() { | ||
| double actual = calculate.calculate(-5, 0, Calculate.EXPONENTIATION); | ||
| double answer = 1; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToPower_Ok() { | ||
| double actual = calculate.calculate(0, 0, Calculate.EXPONENTIATION); | ||
| double answer = 1; | ||
| assertEquals(answer,actual); | ||
|
|
||
| actual = calculate.calculate(0, 5, Calculate.EXPONENTIATION); | ||
| answer = 0; | ||
| assertEquals(answer,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingZeroToNegativePower_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculate.calculate(0, -5, Calculate.EXPONENTIATION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void illegalOperation_notOk() { | ||
| assertThrows(NoSuchElementException.class, () -> { | ||
| calculate.calculate(0, 0, '&'); | ||
| }); | ||
| } | ||
| } | ||
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.
Maybe IllegalArgumentException would be better here?
