From db1c0d25a01dd6a3023bf8e13ed60d7e6bbdeffb Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 16:30:37 +0200 Subject: [PATCH] added multiplying two ComplexNumbers with imaginary parts --- .../java/com/ugsbo/complexnumcalc/ComplexNumber.java | 4 ++-- .../complexnumcalc/MultiplyComplexNumbersTest.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 37e9a0c..cbb6331 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -100,8 +100,8 @@ public class ComplexNumber { public ComplexNumber multiply(ComplexNumber faktor) { Double productRealPart, productImaginaryPart; - productRealPart = this.realPart * faktor.realPart + this.imaginaryPart * faktor.imaginaryPart * -1.0; - productImaginaryPart = 0.0; + productRealPart = this.realPart * faktor.realPart - this.imaginaryPart * faktor.imaginaryPart; + productImaginaryPart = this.realPart * faktor.imaginaryPart + this.imaginaryPart * faktor.realPart; ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart); diff --git a/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java index 58ae87b..92f007c 100644 --- a/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java +++ b/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java @@ -16,4 +16,15 @@ public class MultiplyComplexNumbersTest { assertTrue("The product is not as expected", product.equals(expected)); } + + @Test + public void multiplyTwoComplexNumbersWithImaginaryPart() { + ComplexNumber firstFaktor = new ComplexNumber(Double.valueOf(5), Double.valueOf(3)); + ComplexNumber secoundFaktor = new ComplexNumber(Double.valueOf(6), Double.valueOf(2)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(24.0), Double.valueOf(28.0)); + + ComplexNumber product = firstFaktor.multiply(secoundFaktor); + + assertTrue("The product is not as expected", product.equals(expected)); + } } \ No newline at end of file