From e8dfdfd5c93e599b56cf9fa0353054461f7a636c Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 18:40:13 +0200 Subject: [PATCH] Added conjugationOf to the ComplexNumber object --- .../ugsbo/complexnumcalc/ComplexNumber.java | 14 ++++++++++++ .../conjugationOfComplexNumbersTest.java | 22 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index ca5fddc..ade991d 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -137,4 +137,18 @@ public class ComplexNumber { return absoluteValue; } + /** + * Calucates the absolute value of this complex number + * @return the absolute value + */ + public ComplexNumber conjugationOf(){ + if(this.imaginaryPart.equals(Double.valueOf(0))){ + return this; + } else { + this.imaginaryPart *= (-1); + return this; + } + + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java new file mode 100644 index 0000000..e996959 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java @@ -0,0 +1,22 @@ +package com.ugsbo.complexnumcalc; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class conjugationOfComplexNumbersTest { + + @Test + public void TheConjugatedComplexNumberOfAComplexNumberWithOnlyARealPartShouldBeTheRealPart_ButItIsNot() { + Double realPart = Double.valueOf(4); + Double imaginaryPart = Double.valueOf(0); + ComplexNumber complexNumber = new ComplexNumber(realPart, imaginaryPart); + ComplexNumber expected = new ComplexNumber(realPart, imaginaryPart); + + ComplexNumber actual = complexNumber.conjugationOf(); + + assertTrue("TheConjugatedComplexNumberOfAComplexNumberWithOnlyARealPartIsTheRealPart", expected.equals(actual)); + //TODO change equals to equalTo + //assertThat("TheConjugatedComplexNumberOfAComplexNumberWithOnlyARealPartIsTheRealPart", expected, equalsTo(actual)); + } +} \ No newline at end of file