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 11final 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+ (imaginary * other .real - real * other .imaginary ) / divisor );
3638 }
3739
3840 double abs () {
@@ -44,14 +46,7 @@ ComplexNumber conjugate() {
4446 }
4547
4648 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 ));
4850 }
4951
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- }
5752}
Original file line number Diff line number Diff line change @@ -28,24 +28,15 @@ 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 }
4638
4739 ComplexNumber exponentialOf () {
4840 throw new UnsupportedOperationException ("Delete this statement and write your own implementation." );
4941 }
50-
5142}
You can’t perform that action at this time.
0 commit comments