From eef6b9ee6e908cdbe9faa06b086358ffbc329153 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 18:00:49 +0200 Subject: [PATCH] The absolute value of an complex Number with only a real part can is now implemented in the absolutValueOf function --- .../ugsbo/complexnumcalc/ComplexNumber.java | 5 ++++ .../AbsoluteValueOfComplexNumbersTest.java | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 3f50272..90769e5 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -128,4 +128,9 @@ public class ComplexNumber { return qoutient; } + public Double absolutValueOf(){ + Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2)) ; + return absoluteValue; + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java new file mode 100644 index 0000000..be70ff6 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java @@ -0,0 +1,29 @@ +package com.ugsbo.complexnumcalc; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +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(); + + assertEquals("The absolute value of an complex number with only an real part should be the absolute value of that real part", expected, actual); + } + + @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); + } +} \ No newline at end of file