From 8252779c8f4184183e006701945a5427fe50b27f Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 15:40:06 +0200 Subject: [PATCH] Correct a Typo and added substract function to the ComplexNumber Object --- .../ugsbo/complexnumcalc/ComplexNumber.java | 17 +++++++++++ .../complexnumcalc/AddComplexNumbersTest.java | 8 ++--- .../SubstractComplexNumbersTest.java | 30 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/ugsbo/complexnumcalc/SubstractComplexNumbersTest.java diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 7350d89..3d35f8d 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/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; + } + } \ 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 index 4c1d395..644406f 100644 --- a/src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java +++ b/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)); } 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..cc42978 --- /dev/null +++ b/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)); + } +} \ No newline at end of file