Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
(imaginary * other.real - real * other.imaginary) / divisor);
}

double abs() {
Expand All @@ -44,14 +46,6 @@ 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);
return new ComplexNumber(Math.exp(real) * Math.cos(imaginary), Math.exp(real) * Math.sin(imaginary));
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make check that there is an empty line at the end? Without it, Checkstyle fails.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll do it right now!

Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,15 @@ 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.");
}

ComplexNumber exponentialOf() {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

}
Loading