Browse Source

Correct a Typo and added substract function to the ComplexNumber Object

featureKomplexNumberCalculator
Lukas Reichwein 6 years ago
parent
commit
8252779c8f
  1. 17
      src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java
  2. 8
      src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java
  3. 30
      src/test/java/com/ugsbo/complexnumcalc/SubstractComplexNumbersTest.java

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

@ -74,4 +74,21 @@ public class ComplexNumber {
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;
}
}

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

@ -9,10 +9,10 @@ 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 secoundAddend = new ComplexNumber(Double.valueOf(6), Double.valueOf(0));
ComplexNumber expected = new ComplexNumber(Double.valueOf(11), Double.valueOf(0));
ComplexNumber sum = firstAddend.add(SecoundAddend);
ComplexNumber sum = firstAddend.add(secoundAddend);
assertTrue("Dont sum to the sum", sum.equals(expected));
}
@ -20,10 +20,10 @@ public class AddComplexNumbersTest {
@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 secoundAddend = new ComplexNumber(Double.valueOf(6), Double.valueOf(4));
ComplexNumber expected = new ComplexNumber(Double.valueOf(11), Double.valueOf(7));
ComplexNumber sum = firstAddend.add(SecoundAddend);
ComplexNumber sum = firstAddend.add(secoundAddend);
assertTrue("Dont sum to the sum", sum.equals(expected));
}

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

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