From 999ff99f97ca8510c1417c310c828710ced4c524 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Sun, 7 Apr 2024 21:50:25 +0200 Subject: [PATCH] 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; + } }