From ac21b68eacc1073e3601f11b5e5b6addb6663ef3 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 16:09:02 +0200 Subject: [PATCH] Added the multiply functionality for ComplexNumbers it can multiply ComplexNumbers without an Imaginary part --- .../ugsbo/complexnumcalc/ComplexNumber.java | 17 +++++++++++++++++ .../MultiplyComplexNumbersTest.java | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 3d35f8d..37e9a0c 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -91,4 +91,21 @@ public class ComplexNumber { return differenz; } + /** + * Multiplies the faktor with this Instance. + * + * @param faktor The ComplexNumber by wich this Instance will get multiplyed + * @return The product of this Instance and the faktor + */ + public ComplexNumber multiply(ComplexNumber faktor) { + Double productRealPart, productImaginaryPart; + + productRealPart = this.realPart * faktor.realPart + this.imaginaryPart * faktor.imaginaryPart * -1.0; + productImaginaryPart = 0.0; + + ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart); + + return product; + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java new file mode 100644 index 0000000..58ae87b --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java @@ -0,0 +1,19 @@ +package com.ugsbo.complexnumcalc; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class MultiplyComplexNumbersTest { + + @Test + public void multiplyTwoComplexNumbersWithoutImaginaryPart() { + ComplexNumber firstFaktor = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + ComplexNumber secoundFaktor = new ComplexNumber(Double.valueOf(6), Double.valueOf(0)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(30), Double.valueOf(0)); + + ComplexNumber product = firstFaktor.multiply(secoundFaktor); + + assertTrue("The product is not as expected", product.equals(expected)); + } +} \ No newline at end of file