From 24da1c0969968d17cb8381c002711c538e5276e1 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Tue, 16 Jul 2019 15:23:42 +0200 Subject: [PATCH] Added add function to the ComplexNumber Object --- .../ugsbo/complexnumcalc/ComplexNumber.java | 18 +++++++++++ .../complexnumcalc/AddComplexNumbersTest.java | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/test/java/com/ugsbo/complexnumcalc/AddComplexNumbersTest.java diff --git a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java b/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java index 3eb2db5..7350d89 100644 --- a/src/main/java/com/ugsbo/complexnumcalc/ComplexNumber.java +++ b/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; + } + } \ 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 new file mode 100644 index 0000000..4c1d395 --- /dev/null +++ b/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)); + } +} \ No newline at end of file