Browse Source

added multiplying two ComplexNumbers with imaginary parts

featureKomplexNumberCalculator
Lukas Reichwein 5 years ago
parent
commit
db1c0d25a0
  1. 4
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 11
      src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java

4
src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java

@ -100,8 +100,8 @@ public class ComplexNumber {
public ComplexNumber multiply(ComplexNumber faktor) { public ComplexNumber multiply(ComplexNumber faktor) {
Double productRealPart, productImaginaryPart; 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); ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart);

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