Browse Source

Added equal function to the ComplexNumber Object

featureKomplexNumberCalculator
Lukas Reichwein 5 years ago
parent
commit
793c47bad1
  1. 34
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 69
      src/test/java/com/ugsbo/complexnumcalc/EqualsComplexNumversTest.java

34
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;
}
}
}

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