From c944722402818a3e6a4b60eadec3bbdd4a840d03 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:06:27 +0200 Subject: [PATCH 01/13] Added sample code for complex number addition and multiplication --- .../ComplexNumberAddMultiplyUnitTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberAddMultiplyUnitTest.java 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/ComplexNumberAddMultiplyUnitTest.java new file mode 100644 index 0000000000..4a14f83e45 --- /dev/null +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberAddMultiplyUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.complexnumbers; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +class ComplexNumber { + public long real; + public long imaginary; + + public ComplexNumber(long a, long b) { + this.real = a; + this.imaginary = b; + } + + public ComplexNumber(String complexNumberStr) { + Pattern pattern = Pattern.compile("(-?\\d+)\\s*[+]\\s*(-?\\d+)\\s*i|(-?\\d+)\\s*i\\s*[+]\\s*(-?\\d+)"); + Matcher matcher = pattern.matcher(complexNumberStr); + if (matcher.find()) { + // Extract real and imaginary parts + long realPart; + long imaginaryPart; + if (matcher.group(1) != null && matcher.group(2) != null) { + realPart = Long.parseLong(matcher.group(1)); // Group 1 is the real part + imaginaryPart = Long.parseLong(matcher.group(2)); // Group 2 is the imaginary part + } else { + realPart = Long.parseLong(matcher.group(4)); // Group 4 is the real part + imaginaryPart = Long.parseLong(matcher.group(3)); // Group 3 is the imaginary part + } + this.real = realPart; + this.imaginary = imaginaryPart; + } else { + throw new IllegalArgumentException("String does not match the complex number pattern of `a+bi` or `bi+a`."); + } + } + + public long getReal() { + return real; + } + + public long getImaginary() { + return imaginary; + } + + public String toString() { + return real + "+" + imaginary + "i"; + } + + public ComplexNumber add(ComplexNumber that) { + return new ComplexNumber(real + that.getReal(), imaginary + that.getImaginary()); + } + + 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; + return new ComplexNumber(newReal, newImaginary); + } + + public boolean isSame(ComplexNumber that) { + return this.real == that.real && this.imaginary == that.imaginary; + } + +} + +public class ComplexNumberAddMultiplyUnitTest { + + @ParameterizedTest(name = "Multiplying {0} and {1}") + @CsvSource({ + "3+2i, 1+7i, -11+23i", + "1+1i, 1+1i, 0+2i", + "2i+3, 1+7i, 23i+-11", + "2i +3, 1 + 7i, 23i+ -11", + "0+5i, 3+0i, 0+15i", + "0+0i, -2+0i, 0+0i", + "-3+2i, 1-7i, -23-23i" + }) + public void sum_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 multiplied = complex1.multiply(complex2); + System.out.println(multiplied.toString()); + Assertions.assertTrue(multiplied.isSame(expected)); + } + +} From eed82faf31a6aad33781fbf7678b4f4ebef77500 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:07:38 +0200 Subject: [PATCH 02/13] adjusted test data --- .../complexnumbers/ComplexNumberAddMultiplyUnitTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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/ComplexNumberAddMultiplyUnitTest.java index 4a14f83e45..a15642bbce 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/ComplexNumberAddMultiplyUnitTest.java @@ -72,8 +72,7 @@ public class ComplexNumberAddMultiplyUnitTest { @CsvSource({ "3+2i, 1+7i, -11+23i", "1+1i, 1+1i, 0+2i", - "2i+3, 1+7i, 23i+-11", - "2i +3, 1 + 7i, 23i+ -11", + " 3+2i, 1 + 7i, -11 + 23i ", "0+5i, 3+0i, 0+15i", "0+0i, -2+0i, 0+0i", "-3+2i, 1-7i, -23-23i" From 1ede00ad506b396db60cc3662f12d572f10c38ea Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:35:11 +0200 Subject: [PATCH 03/13] Fixed the issue with some formats --- .../ComplexNumberAddMultiplyUnitTest.java | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) 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/ComplexNumberAddMultiplyUnitTest.java index a15642bbce..b8d6b3266b 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/ComplexNumberAddMultiplyUnitTest.java @@ -17,24 +17,39 @@ class ComplexNumber { this.imaginary = b; } - public ComplexNumber(String complexNumberStr) { - Pattern pattern = Pattern.compile("(-?\\d+)\\s*[+]\\s*(-?\\d+)\\s*i|(-?\\d+)\\s*i\\s*[+]\\s*(-?\\d+)"); - Matcher matcher = pattern.matcher(complexNumberStr); - if (matcher.find()) { - // Extract real and imaginary parts - long realPart; - long imaginaryPart; - if (matcher.group(1) != null && matcher.group(2) != null) { - realPart = Long.parseLong(matcher.group(1)); // Group 1 is the real part - imaginaryPart = Long.parseLong(matcher.group(2)); // Group 2 is the imaginary part + public ComplexNumber(String complexNumberStr1) { + + String complexNumberStr = complexNumberStr1.replaceAll("\\s", ""); + if (complexNumberStr.contains("i")) { + // Split the string based on '+' or '-' + String[] parts = complexNumberStr.split("(?=[+-])"); + + if (parts.length == 1) { + // Only real part (no imaginary part) + if (complexNumberStr.endsWith("i")) { + real = 0; + imaginary = Long.parseLong(parts[0].substring(0, parts[0].length() - 1)); + } else { + real = Long.parseLong(complexNumberStr); + imaginary = 0; + } + } else if (parts.length == 2) { + // Both real and imaginary parts present + real = Long.parseLong(parts[0]); + String imaginaryString = parts[1].substring(0, parts[1].length() - 1); // Remove 'i' + if (imaginaryString.isEmpty()) { + // Only 'i' without coefficient, hence imaginary part is 1 + imaginary = 1; + } else { + imaginary = Long.parseLong(imaginaryString); + } } else { - realPart = Long.parseLong(matcher.group(4)); // Group 4 is the real part - imaginaryPart = Long.parseLong(matcher.group(3)); // Group 3 is the imaginary part + throw new IllegalArgumentException("Invalid complex number format"); } - this.real = realPart; - this.imaginary = imaginaryPart; } else { - throw new IllegalArgumentException("String does not match the complex number pattern of `a+bi` or `bi+a`."); + // Only real part without 'i' + real = Long.parseLong(complexNumberStr); + imaginary = 0; } } @@ -71,18 +86,20 @@ public class ComplexNumberAddMultiplyUnitTest { @ParameterizedTest(name = "Multiplying {0} and {1}") @CsvSource({ "3+2i, 1+7i, -11+23i", + "2, 4, 8", + "2, 4i, 8i", "1+1i, 1+1i, 0+2i", " 3+2i, 1 + 7i, -11 + 23i ", "0+5i, 3+0i, 0+15i", "0+0i, -2+0i, 0+0i", - "-3+2i, 1-7i, -23-23i" + "-3+2i, 1-7i, 11+23i", + "2+4i, 0, 0" }) public void sum_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 multiplied = complex1.multiply(complex2); - System.out.println(multiplied.toString()); Assertions.assertTrue(multiplied.isSame(expected)); } From aabaaf5ec970cb98b5e5c8ab0a51239894fe237d Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:35:59 +0200 Subject: [PATCH 04/13] removed unused imports --- .../complexnumbers/ComplexNumberAddMultiplyUnitTest.java | 4 ---- 1 file changed, 4 deletions(-) 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/ComplexNumberAddMultiplyUnitTest.java index b8d6b3266b..41864d6c86 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/ComplexNumberAddMultiplyUnitTest.java @@ -1,13 +1,9 @@ package com.baeldung.complexnumbers; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - class ComplexNumber { public long real; public long imaginary; From bb499f340c5cdd0e345dfd9e91c1355b822bf1f3 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:40:20 +0200 Subject: [PATCH 05/13] simplified the parsing using regex --- .../ComplexNumberAddMultiplyUnitTest.java | 47 +++++++------------ 1 file changed, 17 insertions(+), 30 deletions(-) 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/ComplexNumberAddMultiplyUnitTest.java index 41864d6c86..13be10c3ef 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/ComplexNumberAddMultiplyUnitTest.java @@ -4,6 +4,9 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + class ComplexNumber { public long real; public long imaginary; @@ -13,39 +16,23 @@ class ComplexNumber { this.imaginary = b; } - public ComplexNumber(String complexNumberStr1) { + public ComplexNumber(String complexNumberStr) { - String complexNumberStr = complexNumberStr1.replaceAll("\\s", ""); - if (complexNumberStr.contains("i")) { - // Split the string based on '+' or '-' - String[] parts = complexNumberStr.split("(?=[+-])"); + Pattern pattern = Pattern.compile("(-?\\d+)?(?:([+-]?\\d+)i)?"); + Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", "")); - if (parts.length == 1) { - // Only real part (no imaginary part) - if (complexNumberStr.endsWith("i")) { - real = 0; - imaginary = Long.parseLong(parts[0].substring(0, parts[0].length() - 1)); - } else { - real = Long.parseLong(complexNumberStr); - imaginary = 0; - } - } else if (parts.length == 2) { - // Both real and imaginary parts present - real = Long.parseLong(parts[0]); - String imaginaryString = parts[1].substring(0, parts[1].length() - 1); // Remove 'i' - if (imaginaryString.isEmpty()) { - // Only 'i' without coefficient, hence imaginary part is 1 - imaginary = 1; - } else { - imaginary = Long.parseLong(imaginaryString); - } - } else { - throw new IllegalArgumentException("Invalid complex number format"); - } + if (matcher.matches()) { + // Extract real and imaginary parts + String realPartStr = matcher.group(1); + String imaginaryPartStr = matcher.group(2); + + // Parse real part (if present) + real = (realPartStr != null) ? Long.parseLong(realPartStr) : 0; + + // Parse imaginary part (if present) + imaginary = (imaginaryPartStr != null) ? Long.parseLong(imaginaryPartStr) : 0; } else { - // Only real part without 'i' - real = Long.parseLong(complexNumberStr); - imaginary = 0; + throw new IllegalArgumentException("Invalid complex number format, supported format is `a+bi`"); } } From 999ff99f97ca8510c1417c310c828710ced4c524 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:50:25 +0200 Subject: [PATCH 06/13] Moved ComplexNumber to main source --- .../complexnumbers/ComplexNumber.java | 57 ++++++++++++++++ .../ComplexNumberAddMultiplyUnitTest.java | 65 ++----------------- 2 files changed, 61 insertions(+), 61 deletions(-) create mode 100644 core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java 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 new file mode 100644 index 0000000000..81bf2793a7 --- /dev/null +++ b/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/complexnumbers/ComplexNumber.java @@ -0,0 +1,57 @@ +package com.baeldung.complexnumbers; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ComplexNumber { + public long real; + public long imaginary; + + public ComplexNumber(long a, long b) { + this.real = a; + this.imaginary = b; + } + + public ComplexNumber(String complexNumberStr) { + + Pattern pattern = Pattern.compile("(-?\\d+)?(?:([+-]?\\d+)i)?"); + Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", "")); + + if (matcher.matches()) { + // Extract real and imaginary parts + String realPartStr = matcher.group(1); + String imaginaryPartStr = matcher.group(2); + + // Parse real part (if present) + real = (realPartStr != null) ? Long.parseLong(realPartStr) : 0; + + // Parse imaginary part (if present) + imaginary = (imaginaryPartStr != null) ? Long.parseLong(imaginaryPartStr) : 0; + } else { + throw new IllegalArgumentException("Invalid complex number format, supported format is `a+bi`"); + } + } + + public long getReal() { + return real; + } + + public long getImaginary() { + return imaginary; + } + + public String toString() { + return real + "+" + imaginary + "i"; + } + + public ComplexNumber add(ComplexNumber that) { + return new ComplexNumber(real + that.getReal(), imaginary + that.getImaginary()); + } + + 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; + return new ComplexNumber(newReal, newImaginary); + } + +} \ No newline at end of file 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/ComplexNumberAddMultiplyUnitTest.java index 13be10c3ef..4a550da895 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/ComplexNumberAddMultiplyUnitTest.java @@ -4,66 +4,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -class ComplexNumber { - public long real; - public long imaginary; - - public ComplexNumber(long a, long b) { - this.real = a; - this.imaginary = b; - } - - public ComplexNumber(String complexNumberStr) { - - Pattern pattern = Pattern.compile("(-?\\d+)?(?:([+-]?\\d+)i)?"); - Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", "")); - - if (matcher.matches()) { - // Extract real and imaginary parts - String realPartStr = matcher.group(1); - String imaginaryPartStr = matcher.group(2); - - // Parse real part (if present) - real = (realPartStr != null) ? Long.parseLong(realPartStr) : 0; - - // Parse imaginary part (if present) - imaginary = (imaginaryPartStr != null) ? Long.parseLong(imaginaryPartStr) : 0; - } else { - throw new IllegalArgumentException("Invalid complex number format, supported format is `a+bi`"); - } - } - - public long getReal() { - return real; - } - - public long getImaginary() { - return imaginary; - } - - public String toString() { - return real + "+" + imaginary + "i"; - } - - public ComplexNumber add(ComplexNumber that) { - return new ComplexNumber(real + that.getReal(), imaginary + that.getImaginary()); - } - - 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; - return new ComplexNumber(newReal, newImaginary); - } - - public boolean isSame(ComplexNumber that) { - return this.real == that.real && this.imaginary == that.imaginary; - } - -} - public class ComplexNumberAddMultiplyUnitTest { @ParameterizedTest(name = "Multiplying {0} and {1}") @@ -83,7 +23,10 @@ public class ComplexNumberAddMultiplyUnitTest { ComplexNumber complex2 = new ComplexNumber(complexStr2); ComplexNumber expected = new ComplexNumber(expectedStr); ComplexNumber multiplied = complex1.multiply(complex2); - Assertions.assertTrue(multiplied.isSame(expected)); + Assertions.assertTrue(isSame(multiplied, expected)); } + public boolean isSame(ComplexNumber result, ComplexNumber expected) { + return result.real == expected.real && result.imaginary == expected.imaginary; + } } From cdf1bcdc0e6ce482ddaa7d6649daf0b91bd7a2d2 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:53:33 +0200 Subject: [PATCH 07/13] Added the test case for complex number addition --- .../ComplexNumberAddMultiplyUnitTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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/ComplexNumberAddMultiplyUnitTest.java index 4a550da895..89fec30c5a 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/ComplexNumberAddMultiplyUnitTest.java @@ -18,12 +18,32 @@ public class ComplexNumberAddMultiplyUnitTest { "-3+2i, 1-7i, 11+23i", "2+4i, 0, 0" }) - public void sum_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + public void multiply_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 multiplied = complex1.multiply(complex2); - Assertions.assertTrue(isSame(multiplied, expected)); + ComplexNumber product = complex1.multiply(complex2); + Assertions.assertTrue(isSame(product, expected)); + } + + @ParameterizedTest(name = "Adding {0} and {1}") + @CsvSource({ + "3+2i, 1+7i, 4+9i", + "2, 4, 6", + "2, 4i, 2+4i", + "1+1i, 1+1i, 2+2i", + " 3+2i, 1 + 7i, 4 + 9i ", + "0+5i, 3+0i, 3+5i", + "0+0i, -2+0i, -2+0i", + "-3+2i, 1-7i, -2-5i", + "2+4i, 0, 2+4i" + }) + public void add_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.add(complex2); + Assertions.assertTrue(isSame(sum, expected)); } public boolean isSame(ComplexNumber result, ComplexNumber expected) { From 9ddceb29f1e64e4d1247cda4c1c4b45f824c8713 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Tue, 9 Apr 2024 09:00:01 +0200 Subject: [PATCH 08/13] 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; } From 423e60577d8ebb4806372bfd13cded0bdfbbad8a Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Tue, 9 Apr 2024 22:04:14 +0200 Subject: [PATCH 09/13] Used records intead of plain class --- .../complexnumbers/ComplexNumber.java | 28 +++++-------------- .../ComplexNumberOperationsUnitTest.java | 26 ++++++++--------- 2 files changed, 20 insertions(+), 34 deletions(-) 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 e2791a0a60..3283b081fe 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 @@ -3,16 +3,9 @@ package com.baeldung.complexnumbers; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class ComplexNumber { - public double real; - public double imaginary; +public record ComplexNumber(double real, double imaginary) { - public ComplexNumber(double a, double b) { - this.real = a; - this.imaginary = b; - } - - public ComplexNumber(String complexNumberStr) { + public static ComplexNumber fromString(String complexNumberStr) { Pattern pattern = Pattern.compile("(-?\\d*\\.?\\d+)?(?:([+-]?\\d*\\.?\\d+)i)?"); Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", "")); @@ -23,29 +16,22 @@ public class ComplexNumber { String imaginaryPartStr = matcher.group(2); // Parse real part (if present) - real = (realPartStr != null) ? Double.parseDouble(realPartStr) : 0; + double real = (realPartStr != null) ? Double.parseDouble(realPartStr) : 0; // Parse imaginary part (if present) - imaginary = (imaginaryPartStr != null) ? Double.parseDouble(imaginaryPartStr) : 0; + double imaginary = (imaginaryPartStr != null) ? Double.parseDouble(imaginaryPartStr) : 0; + return new ComplexNumber(real, imaginary); } else { throw new IllegalArgumentException("Invalid complex number format(" + complexNumberStr + "), supported format is `a+bi`"); } } - public double getReal() { - return real; - } - - public double getImaginary() { - return imaginary; - } - public String toString() { return real + "+" + imaginary + "i"; } public ComplexNumber add(ComplexNumber that) { - return new ComplexNumber(real + that.getReal(), imaginary + that.getImaginary()); + return new ComplexNumber(real + that.real, imaginary + that.imaginary); } public ComplexNumber multiply(ComplexNumber that) { @@ -55,7 +41,7 @@ public class ComplexNumber { } public ComplexNumber subtract(ComplexNumber that) { - return new ComplexNumber(real - that.getReal(), imaginary - that.getImaginary()); + return new ComplexNumber(real - that.real, imaginary - that.imaginary); } public ComplexNumber divide(ComplexNumber that) { diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java index c7f726c80b..b3c95b7346 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java @@ -19,9 +19,9 @@ public class ComplexNumberOperationsUnitTest { "2+4i, 0, 0" }) public void multiply_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 complex1 = ComplexNumber.fromString(complexStr1); + ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); + ComplexNumber expected = ComplexNumber.fromString(expectedStr); ComplexNumber product = complex1.multiply(complex2); Assertions.assertTrue(isSame(product, expected)); } @@ -39,9 +39,9 @@ public class ComplexNumberOperationsUnitTest { "2+4i, 0, 2+4i" }) public void add_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 complex1 = ComplexNumber.fromString(complexStr1); + ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); + ComplexNumber expected = ComplexNumber.fromString(expectedStr); ComplexNumber sum = complex1.add(complex2); Assertions.assertTrue(isSame(sum, expected)); } @@ -59,9 +59,9 @@ public class ComplexNumberOperationsUnitTest { "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 complex1 = ComplexNumber.fromString(complexStr1); + ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); + ComplexNumber expected = ComplexNumber.fromString(expectedStr); ComplexNumber sum = complex1.subtract(complex2); Assertions.assertTrue(isSame(sum, expected)); } @@ -79,14 +79,14 @@ public class ComplexNumberOperationsUnitTest { "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 complex1 = ComplexNumber.fromString(complexStr1); + ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); + ComplexNumber expected = ComplexNumber.fromString(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; + return result.real() == expected.real() && result.imaginary() == expected.imaginary(); } } From 2b430714bb97b0cfd8fceb6b14078806d5d6e4eb Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Wed, 10 Apr 2024 08:11:11 +0200 Subject: [PATCH 10/13] Added condition for divide by zero check --- .../com/baeldung/complexnumbers/ComplexNumber.java | 3 +++ .../ComplexNumberOperationsUnitTest.java | 11 +++++++++++ 2 files changed, 14 insertions(+) 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 3283b081fe..223677b7e8 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 @@ -45,6 +45,9 @@ public record ComplexNumber(double real, double imaginary) { } public ComplexNumber divide(ComplexNumber that) { + if(that.real == 0 && that.imaginary == 0 ){ + throw new ArithmeticException("Division by 0 is now allowed!"); + } 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; diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java index b3c95b7346..c92be1f877 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.complexnumbers; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -86,6 +87,16 @@ public class ComplexNumberOperationsUnitTest { Assertions.assertTrue(isSame(sum, expected)); } + @Test + public void check_divide_by_zero() { + ComplexNumber complex1 = new ComplexNumber(1, 1); + ComplexNumber zero = new ComplexNumber(0, 0); + Exception exception = Assertions.assertThrows(ArithmeticException.class, () -> { + complex1.divide(zero); + }); + Assertions.assertEquals(exception.getMessage(), "Division by 0 is now allowed!"); + } + public boolean isSame(ComplexNumber result, ComplexNumber expected) { return result.real() == expected.real() && result.imaginary() == expected.imaginary(); } From 443829bc4f0a5b0c02d1e57f1fd58960c45300dc Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Wed, 10 Apr 2024 21:57:04 +0200 Subject: [PATCH 11/13] Added condition for divide by zero check --- .../main/java/com/baeldung/complexnumbers/ComplexNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 223677b7e8..8e509cfbbb 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 @@ -46,7 +46,7 @@ public record ComplexNumber(double real, double imaginary) { public ComplexNumber divide(ComplexNumber that) { if(that.real == 0 && that.imaginary == 0 ){ - throw new ArithmeticException("Division by 0 is now allowed!"); + throw new ArithmeticException("Division by 0 is not allowed!"); } double c2d2 = Math.pow(that.real, 2) + Math.pow(that.imaginary, 2); double newReal = (this.real * that.real + this.imaginary * that.imaginary) / c2d2; From 0b456e3b4e16c74bbe02a16699a96d679f90ad65 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Wed, 10 Apr 2024 22:43:46 +0200 Subject: [PATCH 12/13] Added condition for divide by zero check --- .../complexnumbers/ComplexNumberOperationsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java index c92be1f877..f85de5caf1 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java @@ -94,7 +94,7 @@ public class ComplexNumberOperationsUnitTest { Exception exception = Assertions.assertThrows(ArithmeticException.class, () -> { complex1.divide(zero); }); - Assertions.assertEquals(exception.getMessage(), "Division by 0 is now allowed!"); + Assertions.assertEquals(exception.getMessage(), "Division by 0 is not allowed!"); } public boolean isSame(ComplexNumber result, ComplexNumber expected) { From 86fa1a3a5f588c667bd292ac24e5ed5207631ba9 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 14 Apr 2024 08:22:03 +0200 Subject: [PATCH 13/13] rename test to follow coding standard --- .../ComplexNumberOperationsUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java index f85de5caf1..b2b7253fb8 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/complexnumbers/ComplexNumberOperationsUnitTest.java @@ -19,7 +19,7 @@ public class ComplexNumberOperationsUnitTest { "-3+2i, 1-7i, 11+23i", "2+4i, 0, 0" }) - public void multiply_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + public void givenTwoComplexNumbers_multiplyAndGetResult(String complexStr1, String complexStr2, String expectedStr) { ComplexNumber complex1 = ComplexNumber.fromString(complexStr1); ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); ComplexNumber expected = ComplexNumber.fromString(expectedStr); @@ -39,7 +39,7 @@ public class ComplexNumberOperationsUnitTest { "-3+2i, 1-7i, -2-5i", "2+4i, 0, 2+4i" }) - public void add_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + public void givenTwoComplexNumbers_addThemAndGetResult(String complexStr1, String complexStr2, String expectedStr) { ComplexNumber complex1 = ComplexNumber.fromString(complexStr1); ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); ComplexNumber expected = ComplexNumber.fromString(expectedStr); @@ -59,7 +59,7 @@ public class ComplexNumberOperationsUnitTest { "-3+2i, 1-7i, -4+9i", "2+4i, 0, 2+4i" }) - public void subtract_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { + public void givenTwoComplexNumbers_subtractAndGetResult(String complexStr1, String complexStr2, String expectedStr) { ComplexNumber complex1 = ComplexNumber.fromString(complexStr1); ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); ComplexNumber expected = ComplexNumber.fromString(expectedStr); @@ -79,7 +79,7 @@ public class ComplexNumberOperationsUnitTest { "-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) { + public void givenTwoComplexNumbers_divideThemAndGetResult(String complexStr1, String complexStr2, String expectedStr) { ComplexNumber complex1 = ComplexNumber.fromString(complexStr1); ComplexNumber complex2 = ComplexNumber.fromString(complexStr2); ComplexNumber expected = ComplexNumber.fromString(expectedStr); @@ -88,7 +88,7 @@ public class ComplexNumberOperationsUnitTest { } @Test - public void check_divide_by_zero() { + public void givenAComplexNumberAsZero_handleDivideByZeroScenario() { ComplexNumber complex1 = new ComplexNumber(1, 1); ComplexNumber zero = new ComplexNumber(0, 0); Exception exception = Assertions.assertThrows(ArithmeticException.class, () -> {