From 4f3f9399a8b321643b876f54e17ffb0f27a0655b Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 18:09:06 +0200 Subject: [PATCH] Added the functionality to calculate the absolute value of an complex number --- .../ugsbo/complexnumcalc/ComplexNumber.java | 2 +- .../AbsoluteValueOfComplexNumbersTest.java | 46 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 90769e5..b55cc87 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -129,7 +129,7 @@ public class ComplexNumber { } public Double absolutValueOf(){ - Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2)) ; + Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imaginaryPart, 2)) ; return absoluteValue; } diff --git a/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java index be70ff6..48a3d70 100644 --- a/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java +++ b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java @@ -8,7 +8,6 @@ public class AbsoluteValueOfComplexNumbersTest { @Test public void TheAbsoluteValueOfAComplexNumberWithOnlyARealPart_IsNotTHeAbsoluteValueOfTheRealPart() { ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(0)); - Double expected = Double.valueOf(4); Double actual = complexNumber.absolutValueOf(); @@ -19,11 +18,54 @@ public class AbsoluteValueOfComplexNumbersTest { @Test public void TheAbsoluteValueOfAComplexNumberWithOnlyANegativeRealPart_IsNotTheAbsoluteValueOfTheRealPart() { ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(0)); - Double expected = Double.valueOf(4); Double actual = complexNumber.absolutValueOf(); assertEquals("The absolute value of an complex number with only an negative real part should be the absolute value of that real part", expected, actual); } + + @Test + public void TheAbsoluteValueOfAComplexNumber_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativRealPart_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative real part should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativImaginaryPart_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(-3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative imaginary part should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativParts_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(-3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative parts should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } } \ No newline at end of file