File tree Expand file tree Collapse file tree 2 files changed +5
-19
lines changed
exercises/practice/complex-numbers Expand file tree Collapse file tree 2 files changed +5
-19
lines changed Original file line number Diff line number Diff line change 1
1
final class ComplexNumber {
2
2
3
3
private final double real ;
4
-
5
4
private final double imaginary ;
6
5
7
6
ComplexNumber (double real , double imaginary ) {
@@ -32,7 +31,10 @@ ComplexNumber subtract(ComplexNumber other) {
32
31
}
33
32
34
33
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
+ (imaginary * other .real - real * other .imaginary ) / divisor );
36
38
}
37
39
38
40
double abs () {
@@ -44,14 +46,7 @@ ComplexNumber conjugate() {
44
46
}
45
47
46
48
ComplexNumber exponentialOf () {
47
- return new ComplexNumber (Math .cos ( imaginary ), Math .sin (imaginary )). multiply ( Math .exp (real ));
49
+ return new ComplexNumber (Math .exp ( real ) * Math .cos (imaginary ), Math .exp (real ) * Math . sin ( imaginary ));
48
50
}
49
51
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
52
}
Original file line number Diff line number Diff line change @@ -28,24 +28,15 @@ ComplexNumber multiply(ComplexNumber other) {
28
28
throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
29
29
}
30
30
31
- ComplexNumber multiply (double factor ) {
32
- throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
33
- }
34
-
35
31
ComplexNumber divide (ComplexNumber other ) {
36
32
throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
37
33
}
38
34
39
- ComplexNumber divide (double divisor ) {
40
- throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
41
- }
42
-
43
35
ComplexNumber conjugate () {
44
36
throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
45
37
}
46
38
47
39
ComplexNumber exponentialOf () {
48
40
throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
49
41
}
50
-
51
42
}
You can’t perform that action at this time.
0 commit comments