From a968529640d87083e5f729a792e97db712c072ab Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sat, 2 Nov 2024 14:59:41 +0530 Subject: [PATCH] Remove multiply and divide methods accepting double from ComplexNumber class --- .../.meta/src/reference/java/ComplexNumber.java | 16 +++++----------- .../src/main/java/ComplexNumber.java | 8 -------- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/exercises/practice/complex-numbers/.meta/src/reference/java/ComplexNumber.java b/exercises/practice/complex-numbers/.meta/src/reference/java/ComplexNumber.java index 3bf393ba0..55b2318c8 100644 --- a/exercises/practice/complex-numbers/.meta/src/reference/java/ComplexNumber.java +++ b/exercises/practice/complex-numbers/.meta/src/reference/java/ComplexNumber.java @@ -1,7 +1,6 @@ final class ComplexNumber { private final double real; - private final double imaginary; ComplexNumber(double real, double imaginary) { @@ -32,7 +31,10 @@ ComplexNumber subtract(ComplexNumber other) { } ComplexNumber divide(ComplexNumber other) { - return this.multiply(other.conjugate()).divide(Math.pow(other.abs(), 2)); + double divisor = Math.pow(other.real, 2) + Math.pow(other.imaginary, 2); + return new ComplexNumber( + (real * other.real + imaginary * other.imaginary) / divisor, + (real * other.imaginary - imaginary * other.real) / divisor); } double abs() { @@ -46,12 +48,4 @@ ComplexNumber conjugate() { ComplexNumber exponentialOf() { return new ComplexNumber(Math.cos(imaginary), Math.sin(imaginary)).multiply(Math.exp(real)); } - - private ComplexNumber divide(double factor) { - return new ComplexNumber(real / factor, imaginary / factor); - } - - private ComplexNumber multiply(double factor) { - return new ComplexNumber(factor * real, factor * imaginary); - } -} +} \ No newline at end of file diff --git a/exercises/practice/complex-numbers/src/main/java/ComplexNumber.java b/exercises/practice/complex-numbers/src/main/java/ComplexNumber.java index 6a47ada17..65ad8619b 100644 --- a/exercises/practice/complex-numbers/src/main/java/ComplexNumber.java +++ b/exercises/practice/complex-numbers/src/main/java/ComplexNumber.java @@ -28,18 +28,10 @@ ComplexNumber multiply(ComplexNumber other) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } - ComplexNumber multiply(double factor) { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - ComplexNumber divide(ComplexNumber other) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } - ComplexNumber divide(double divisor) { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - ComplexNumber conjugate() { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); }