From 9ddceb29f1e64e4d1247cda4c1c4b45f824c8713 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Tue, 9 Apr 2024 09:00:01 +0200 Subject: [PATCH] Added subtract and divide operations --- .../complexnumbers/ComplexNumber.java | 33 ++++++++++----- ...a => ComplexNumberOperationsUnitTest.java} | 42 ++++++++++++++++++- 2 files changed, 63 insertions(+), 12 deletions(-) rename core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/{ComplexNumberAddMultiplyUnitTest.java => ComplexNumberOperationsUnitTest.java} (54%) diff --git a/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java b/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java index 81bf2793a7..e2791a0a60 100644 --- a/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java +++ b/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java @@ -4,17 +4,17 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class ComplexNumber { - public long real; - public long imaginary; + public double real; + public double imaginary; - public ComplexNumber(long a, long b) { + public ComplexNumber(double a, double b) { this.real = a; this.imaginary = b; } public ComplexNumber(String complexNumberStr) { - Pattern pattern = Pattern.compile("(-?\\d+)?(?:([+-]?\\d+)i)?"); + Pattern pattern = Pattern.compile("(-?\\d*\\.?\\d+)?(?:([+-]?\\d*\\.?\\d+)i)?"); Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", "")); if (matcher.matches()) { @@ -23,20 +23,20 @@ public class ComplexNumber { String imaginaryPartStr = matcher.group(2); // Parse real part (if present) - real = (realPartStr != null) ? Long.parseLong(realPartStr) : 0; + real = (realPartStr != null) ? Double.parseDouble(realPartStr) : 0; // Parse imaginary part (if present) - imaginary = (imaginaryPartStr != null) ? Long.parseLong(imaginaryPartStr) : 0; + imaginary = (imaginaryPartStr != null) ? Double.parseDouble(imaginaryPartStr) : 0; } else { - throw new IllegalArgumentException("Invalid complex number format, supported format is `a+bi`"); + throw new IllegalArgumentException("Invalid complex number format(" + complexNumberStr + "), supported format is `a+bi`"); } } - public long getReal() { + public double getReal() { return real; } - public long getImaginary() { + public double getImaginary() { return imaginary; } @@ -49,8 +49,19 @@ public class ComplexNumber { } public ComplexNumber multiply(ComplexNumber that) { - long newReal = this.real * that.real - this.imaginary * that.imaginary; - long newImaginary = this.real * that.imaginary + this.imaginary * that.real; + double newReal = this.real * that.real - this.imaginary * that.imaginary; + double newImaginary = this.real * that.imaginary + this.imaginary * that.real; + return new ComplexNumber(newReal, newImaginary); + } + + public ComplexNumber subtract(ComplexNumber that) { + return new ComplexNumber(real - that.getReal(), imaginary - that.getImaginary()); + } + + public ComplexNumber divide(ComplexNumber that) { + double c2d2 = Math.pow(that.real, 2) + Math.pow(that.imaginary, 2); + double newReal = (this.real * that.real + this.imaginary * that.imaginary) / c2d2; + double newImaginary = (this.imaginary * that.real - this.real * that.imaginary) / c2d2; return new ComplexNumber(newReal, newImaginary); } diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberAddMultiplyUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java similarity index 54% rename from core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberAddMultiplyUnitTest.java rename to core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java index 89fec30c5a..c7f726c80b 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberAddMultiplyUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -public class ComplexNumberAddMultiplyUnitTest { +public class ComplexNumberOperationsUnitTest { @ParameterizedTest(name = "Multiplying {0} and {1}") @CsvSource({ @@ -46,6 +46,46 @@ public class ComplexNumberAddMultiplyUnitTest { Assertions.assertTrue(isSame(sum, expected)); } + @ParameterizedTest(name = "Subtracting {0} and {1}") + @CsvSource({ + "3+2i, 1+7i, 2-5i", + "2, 4, -2", + "2, 4i, 2-4i", + "1+1i, 1+1i, 0", + " 3+ 2i, 1+ 7i, 2-5i", + "0+5i, 3+0i, -3+5i", + "0+0i, -2+0i, 2+0i", + "-3+2i, 1-7i, -4+9i", + "2+4i, 0, 2+4i" + }) + public void subtract_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + ComplexNumber complex1 = new ComplexNumber(complexStr1); + ComplexNumber complex2 = new ComplexNumber(complexStr2); + ComplexNumber expected = new ComplexNumber(expectedStr); + ComplexNumber sum = complex1.subtract(complex2); + Assertions.assertTrue(isSame(sum, expected)); + } + + @ParameterizedTest(name = "Dividing {0} and {1}") + @CsvSource({ + "3+2i, 1+7i, 0.34-0.38i", + "2, 4, 0.5", + "2, 4i, 0-0.5i", + "1+1i, 1+1i, 1", + "3 + 2i, 1 + 7i, 0.34-0.38i", + "0+5i, 3+0i, 0+1.6666666666666667i", + "0+0i, -2+0i, 0+0i", + "-3+2i, 1-7i, -0.34-0.38i", + "2+4i, 1, 2+4i" + }) + public void divide_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + ComplexNumber complex1 = new ComplexNumber(complexStr1); + ComplexNumber complex2 = new ComplexNumber(complexStr2); + ComplexNumber expected = new ComplexNumber(expectedStr); + ComplexNumber sum = complex1.divide(complex2); + Assertions.assertTrue(isSame(sum, expected)); + } + public boolean isSame(ComplexNumber result, ComplexNumber expected) { return result.real == expected.real && result.imaginary == expected.imaginary; }