Browse Source

Added the multiply functionality for ComplexNumbers it can multiply ComplexNumbers without an Imaginary part

featureKomplexNumberCalculator
Lukas Reichwein 5 years ago
parent
commit
ac21b68eac
  1. 17
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 19
      src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java

17
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;
}
}

19
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));
}
}
Loading…
Cancel
Save