diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index dab8117..3eb2db5 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java @@ -1,15 +1,15 @@ package com.ugsbo.complexnumcalc; public class ComplexNumber { - private int realPart; - private int imaginaryPart; + private Double realPart; + private Double imaginaryPart; /** - * @param realPart The real part of the complex Number + * @param realPart The real part of the complex Number * @param imaginaryPart The imaginary part of the complex Number */ - public ComplexNumber(int realPart, int imaginaryPart) { + public ComplexNumber(Double realPart, Double imaginaryPart) { this.realPart = realPart; this.imaginaryPart = imaginaryPart; } @@ -17,29 +17,43 @@ public class ComplexNumber { /** * @return the realPart */ - public int getRealPart() { + public Double getRealPart() { return realPart; } /** * @param realPart the realPart to set */ - public void setRealPart(int realPart) { + public void setRealPart(Double realPart) { this.realPart = realPart; } /** * @return the imaginaryPart */ - public int getImaginaryPart() { + public Double getImaginaryPart() { return imaginaryPart; } /** * @param imaginaryPart the imaginaryPart to set */ - public void setImaginaryPart(int imaginaryPart) { + 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 + */ + public boolean equals(ComplexNumber complexNumber) { + if (this.realPart.equals(complexNumber.realPart) && this.imaginaryPart.equals(complexNumber.imaginaryPart)) { + return true; + } else { + return false; + } + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumversTest.java b/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumversTest.java new file mode 100644 index 0000000..16a02c8 --- /dev/null +++ b/src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumversTest.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 EqualsComplexNumversTest { + + @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