Skip to content

Commit a968529

Browse files
committed
Remove multiply and divide methods accepting double from ComplexNumber class
1 parent c0f9020 commit a968529

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

exercises/practice/complex-numbers/.meta/src/reference/java/ComplexNumber.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
final class ComplexNumber {
22

33
private final double real;
4-
54
private final double imaginary;
65

76
ComplexNumber(double real, double imaginary) {
@@ -32,7 +31,10 @@ ComplexNumber subtract(ComplexNumber other) {
3231
}
3332

3433
ComplexNumber divide(ComplexNumber other) {
35-
return this.multiply(other.conjugate()).divide(Math.pow(other.abs(), 2));
34+
double divisor = Math.pow(other.real, 2) + Math.pow(other.imaginary, 2);
35+
return new ComplexNumber(
36+
(real * other.real + imaginary * other.imaginary) / divisor,
37+
(real * other.imaginary - imaginary * other.real) / divisor);
3638
}
3739

3840
double abs() {
@@ -46,12 +48,4 @@ ComplexNumber conjugate() {
4648
ComplexNumber exponentialOf() {
4749
return new ComplexNumber(Math.cos(imaginary), Math.sin(imaginary)).multiply(Math.exp(real));
4850
}
49-
50-
private ComplexNumber divide(double factor) {
51-
return new ComplexNumber(real / factor, imaginary / factor);
52-
}
53-
54-
private ComplexNumber multiply(double factor) {
55-
return new ComplexNumber(factor * real, factor * imaginary);
56-
}
57-
}
51+
}

exercises/practice/complex-numbers/src/main/java/ComplexNumber.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ ComplexNumber multiply(ComplexNumber other) {
2828
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
2929
}
3030

31-
ComplexNumber multiply(double factor) {
32-
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
33-
}
34-
3531
ComplexNumber divide(ComplexNumber other) {
3632
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
3733
}
3834

39-
ComplexNumber divide(double divisor) {
40-
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
41-
}
42-
4335
ComplexNumber conjugate() {
4436
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
4537
}

0 commit comments

Comments
 (0)