diff --git a/pom.xml b/pom.xml index 65d9421..88b3b5b 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,12 @@ 4.11 test + + org.hamcrest + hamcrest-junit + 2.0.0.0 + test + org.mockito diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java new file mode 100644 index 0000000..b05fea2 --- /dev/null +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -0,0 +1,156 @@ +package com.ugsbo.complexnumcalc; + +public class ComplexNumber { + private Double realPart; + private Double imaginaryPart; + + /** + * @param realPart The real part of the complex Number + * @param imaginaryPart The imaginary part of the complex Number + */ + + public ComplexNumber(Double realPart, Double imaginaryPart) { + this.realPart = realPart; + this.imaginaryPart = imaginaryPart; + } + + /** + * @return the realPart + */ + public Double getRealPart() { + return realPart; + } + + /** + * @param realPart the realPart to set + */ + public void setRealPart(Double realPart) { + this.realPart = realPart; + } + + /** + * @return the imaginaryPart + */ + public Double getImaginaryPart() { + return imaginaryPart; + } + + /** + * @param imaginaryPart the imaginaryPart to set + */ + public void setImaginaryPart(Double imaginaryPart) { + this.imaginaryPart = imaginaryPart; + } + + /** + * Checks if the given complex Number is equal to this object. + * + * @param complexNumber The number wich gets compared with this Instance + * @return True if the complex Numbers are Equal + */ + @Override + public boolean equals(Object complexNumber) { + if (complexNumber instanceof ComplexNumber){ + ComplexNumber that = (ComplexNumber) complexNumber; + return this.realPart.equals(that.realPart) && this.imaginaryPart.equals(that.imaginaryPart); + } else { + return false; + } + } + + /** + * Adds two complex Numbers together. + * + * @param addend The complex Number. + * @return The result of adding the two complex Numbers together, as a conplex + * Number. + */ + public ComplexNumber add(ComplexNumber addend) { + Double sumRealPart, sumImaginaryPart; + + sumRealPart = this.realPart + addend.realPart; + sumImaginaryPart = this.imaginaryPart + addend.imaginaryPart; + + ComplexNumber sum = new ComplexNumber(sumRealPart, sumImaginaryPart); + + return sum; + } + + /** + * Substracts the Subtrahend form this instance. + * + * @param subtrahend The Number wich will be substracted form the Minuend + * @return The Differenz of the Minuend and Subtrahend. + */ + public ComplexNumber substract(ComplexNumber subtrahend) { + Double differenzRealPart, differenzImaginaryPart; + + differenzRealPart = this.realPart - subtrahend.realPart; + differenzImaginaryPart = this.imaginaryPart - subtrahend.imaginaryPart; + + ComplexNumber differenz = new ComplexNumber(differenzRealPart, differenzImaginaryPart); + + 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; + productImaginaryPart = this.realPart * faktor.imaginaryPart + this.imaginaryPart * faktor.realPart; + + ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart); + + 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; + } + + /** + * Calucates the absolute value of this complex number + * @return the absolute value + */ + public Double absolutValueOf(){ + Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imaginaryPart, 2)) ; + return absoluteValue; + } + + /** + * Calucates the absolute value of this complex number + * @return the absolute value + */ + public ComplexNumber conjugationOf(){ + if(this.imaginaryPart.equals(Double.valueOf(0))){ + return this; + } else { + this.imaginaryPart *= (-1); + return this; + } + + } + +} \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java new file mode 100644 index 0000000..594ce13 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/AbsoluteValueOfComplexNumbersTest.java @@ -0,0 +1,71 @@ +package com.ugsbo.complexnumcalc; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class AbsoluteValueOfComplexNumbersTest { + + @Test + public void TheAbsoluteValueOfAComplexNumberWithOnlyARealPart_IsNotTheAbsoluteValueOfTheRealPart() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(0)); + Double expected = Double.valueOf(4); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with only an real part should be the absolute value of that real part", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithOnlyANegativeRealPart_IsNotTheAbsoluteValueOfTheRealPart() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(0)); + Double expected = Double.valueOf(4); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with only an negative real part should be the absolute value of that real part", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumber_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativRealPart_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative real part should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativImaginaryPart_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(4), Double.valueOf(-3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative imaginary part should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } + + @Test + public void TheAbsoluteValueOfAComplexNumberWithNegativParts_IsNotTheAbsoluteValueOfIt() { + ComplexNumber complexNumber = new ComplexNumber(Double.valueOf(-4), Double.valueOf(-3)); + Double expected = Double.valueOf(5); + + Double actual = complexNumber.absolutValueOf(); + + assertEquals("The absolute value of an complex number with negative parts should be the square root of the sum of real " + + "part times real part and imaginary part times imaginary part ", expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java new file mode 100644 index 0000000..6197380 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java @@ -0,0 +1,31 @@ +package com.ugsbo.complexnumcalc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +import org.junit.Test; + +public class AddComplexNumbersTest { + + @Test + public void addingTwoComplexNumbersWithoutImaginaryPart() { + ComplexNumber firstAddend = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + ComplexNumber secoundAddend = new ComplexNumber(Double.valueOf(6), Double.valueOf(0)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(11), Double.valueOf(0)); + + ComplexNumber sum = firstAddend.add(secoundAddend); + + assertThat("Dont sum to the sum", sum, equalTo(expected)); + } + + @Test + public void addingTwoComplexNumbersWithImaginaryPart() { + ComplexNumber firstAddend = new ComplexNumber(Double.valueOf(5), Double.valueOf(3)); + ComplexNumber secoundAddend = new ComplexNumber(Double.valueOf(6), Double.valueOf(4)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(11), Double.valueOf(7)); + + ComplexNumber sum = firstAddend.add(secoundAddend); + + assertThat("Dont sum to the sum", sum, equalTo(expected)); + } +} \ 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..f0794a7 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/DivideComplexNumbersTest.java @@ -0,0 +1,31 @@ +package com.ugsbo.complexnumcalc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +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); + + assertThat("The quotient is not as expected", quotient, equalTo(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); + + assertThat("The quotient is not as expected", quotient, equalTo(expected)); + } +} \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumbersTest.java new file mode 100644 index 0000000..6d0a176 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumbersTest.java @@ -0,0 +1,69 @@ +package com.ugsbo.complexnumcalc; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class EqualsComplexNumbersTest { + + @Test + public void TwoEqualNumbersWithOnlyRealPart_AreNotDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertTrue("TwoEqualNumbersShouldBeEqual", actual); + } + + @Test + public void TwoNotEqualNumbersWithOnlyRealPart_AreDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(6), Double.valueOf(0)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertFalse("TwoNotEqualNumbersShouldNotBeEqual", actual); + } + + @Test + public void TwoEqualNumbersWithOnlyImaginaryPart_AreNotDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(0), Double.valueOf(5)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(0), Double.valueOf(5)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertTrue("TwoEqualComplexNumbersShouldBeEqual", actual); + } + + @Test + public void TwoNotEqualNumbersWithOnlyImaginaryPart_AreDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(0), Double.valueOf(5)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(0), Double.valueOf(6)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertFalse("TwoNotEqualComplexNumbersShouldNotBeEqual", actual); + } + + @Test + public void TwoEqualComplexNumbers_AreNotDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(5)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(5)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertTrue("TwoEqualComplexNumbersShouldBeEqual", actual); + } + + @Test + public void TwoNotEqualComplexNumbers_AreDetectedAsEqual() { + ComplexNumber firstNumber = new ComplexNumber(Double.valueOf(5), Double.valueOf(5)); + ComplexNumber secoundNumber = new ComplexNumber(Double.valueOf(6), Double.valueOf(6)); + + boolean actual = firstNumber.equals(secoundNumber); + + assertFalse("TwoNotEqualComplexNumbersShouldNotBeEqual", actual); + } +} \ 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..96aed77 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/MultiplyComplexNumbersTest.java @@ -0,0 +1,31 @@ +package com.ugsbo.complexnumcalc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +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); + + assertThat("The product is not as expected", product, equalTo(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); + + assertThat("The product is not as expected", product, equalTo(expected)); + } +} \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/SubstractComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/SubstractComplexNumbersTest.java new file mode 100644 index 0000000..2df14c3 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/SubstractComplexNumbersTest.java @@ -0,0 +1,31 @@ +package com.ugsbo.complexnumcalc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +import org.junit.Test; + +public class SubstractComplexNumbersTest { + + @Test + public void substractTwoComplexNumbersWithoutImaginaryPart() { + ComplexNumber minuend = new ComplexNumber(Double.valueOf(5), Double.valueOf(0)); + ComplexNumber subtrahend = new ComplexNumber(Double.valueOf(6), Double.valueOf(0)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(-1), Double.valueOf(0)); + + ComplexNumber difference = minuend.substract(subtrahend); + + assertThat("The difference is not as expected", difference, equalTo(expected)); + } + + @Test + public void substractTwoComplexNumbersWithImaginaryPart() { + ComplexNumber minuend = new ComplexNumber(Double.valueOf(5), Double.valueOf(5)); + ComplexNumber subtrahend = new ComplexNumber(Double.valueOf(6), Double.valueOf(4)); + ComplexNumber expected = new ComplexNumber(Double.valueOf(-1), Double.valueOf(1)); + + ComplexNumber difference = minuend.substract(subtrahend); + + assertThat("The difference is not as expected", difference, equalTo(expected)); + } +} \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java b/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java new file mode 100644 index 0000000..457ad39 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/conjugationOfComplexNumbersTest.java @@ -0,0 +1,33 @@ +package com.ugsbo.complexnumcalc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +import org.junit.Test; + +public class conjugationOfComplexNumbersTest { + + @Test + public void TheConjugatedComplexNumberOfAComplexNumberWithOnlyARealPartShouldBeTheRealPart_ButItIsNot() { + Double realPart = Double.valueOf(4); + Double imaginaryPart = Double.valueOf(0); + ComplexNumber complexNumber = new ComplexNumber(realPart, imaginaryPart); + ComplexNumber expected = new ComplexNumber(realPart, imaginaryPart); + + ComplexNumber actual = complexNumber.conjugationOf(); + + assertThat("The conjugated complex Number of a complex number with only a real part is the real part", expected, equalTo(actual)); + } + + @Test + public void TheConjugatedComplexNumberOfAComplexNumberShouldBeTheComplexNumberWithSwapedSignImaginaryPart_ButItIsNot() { + Double realPart = Double.valueOf(4); + Double imaginaryPart = Double.valueOf(5); + ComplexNumber complexNumber = new ComplexNumber(realPart, imaginaryPart); + ComplexNumber expected = new ComplexNumber(realPart, -imaginaryPart); + + ComplexNumber actual = complexNumber.conjugationOf(); + + assertThat("The conjugated complex number of a complex number is the complex number with swaped sign in the imaginary part", expected, equalTo(actual)); + } +} \ No newline at end of file