diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index cbb6331..3f50272 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/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; + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/DivideComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/DivideComplexNumbersTest.java new file mode 100644 index 0000000..fd0e2e4 --- /dev/null +++ b/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)); + } +} \ No newline at end of file