Browse Source

Added add function to the ComplexNumber Object

featureKomplexNumberCalculator
Lukas Reichwein 5 years ago
parent
commit
24da1c0969
  1. 18
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 30
      src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java

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

@ -56,4 +56,22 @@ public class ComplexNumber {
} }
} }
/**
* 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;
}
} }

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

@ -0,0 +1,30 @@
package com.ugsbo.complexnumcalc;
import static org.junit.Assert.assertTrue;
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);
assertTrue("Dont sum to the sum", sum.equals(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);
assertTrue("Dont sum to the sum", sum.equals(expected));
}
}
Loading…
Cancel
Save