Skip to content

Commit

Permalink
Remove multiply and divide methods accepting double from ComplexNumbe…
Browse files Browse the repository at this point in the history
…r class
  • Loading branch information
jagdish-15 committed Nov 2, 2024
1 parent c0f9020 commit a968529
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
final class ComplexNumber {

private final double real;

private final double imaginary;

ComplexNumber(double real, double imaginary) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down

0 comments on commit a968529

Please sign in to comment.