Moved ComplexNumber to main source

This commit is contained in:
Yadukrishnan 2024-04-07 21:50:25 +02:00
parent bb499f340c
commit 999ff99f97
2 changed files with 61 additions and 61 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}