Browse Source

added division of ComplexNumbers to the ComplexNumber Object.

featureKomplexNumberCalculator
Lukas Reichwein 5 years ago
parent
commit
6757bfe86d
  1. 20
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 30
      src/test/java/com/ugsbo/complexnumcalc/DivideComplexNumbersTest.java

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

@ -108,4 +108,24 @@ public class ComplexNumber {
return product;
}
/**
* Divides the dividend by the divisor, the dividend is this Instance.
*
* @param divisor The ComplexNumber by wich this Instance will get divided
* @return The Qoutient of the Instance and the divisor
*/
public ComplexNumber divide(ComplexNumber divisor) {
Double qoutientRealPart, qoutientImaginaryPart, tempDivisor;
tempDivisor = divisor.realPart * divisor.realPart + divisor.imaginaryPart * divisor.imaginaryPart;
qoutientRealPart = this.realPart * divisor.realPart + this.imaginaryPart * divisor.imaginaryPart;
qoutientImaginaryPart = this.imaginaryPart * divisor.realPart - this.realPart * divisor.imaginaryPart;
qoutientImaginaryPart /= tempDivisor;
qoutientRealPart /= tempDivisor;
ComplexNumber qoutient = new ComplexNumber(qoutientRealPart, qoutientImaginaryPart);
return qoutient;
}
}

30
src/test/java/com/ugsbo/complexnumcalc/DivideComplexNumbersTest.java

@ -0,0 +1,30 @@
package com.ugsbo.complexnumcalc;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class DivideComplexNumbersTest {
@Test
public void divideTwoComplexNumbersWithoutImaginaryPart() {
ComplexNumber dividend = new ComplexNumber(Double.valueOf(30), Double.valueOf(0));
ComplexNumber divisor = new ComplexNumber(Double.valueOf(6), Double.valueOf(0));
ComplexNumber expected = new ComplexNumber(Double.valueOf(5), Double.valueOf(0));
ComplexNumber quotient = dividend.divide(divisor);
assertTrue("The quotient is not as expected", quotient.equals(expected));
}
@Test
public void divideTwoComplexNumbersWithImaginaryPart() {
ComplexNumber dividend = new ComplexNumber(Double.valueOf(30), Double.valueOf(28));
ComplexNumber divisor = new ComplexNumber(Double.valueOf(6), Double.valueOf(2));
ComplexNumber expected = new ComplexNumber(Double.valueOf(5.9), Double.valueOf(2.7));
ComplexNumber quotient = dividend.divide(divisor);
assertTrue("The quotient is not as expected", quotient.equals(expected));
}
}
Loading…
Cancel
Save