This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ComplexNumber { | |
private float realPart; | |
private float complexPart; | |
// create a new instance via static method | |
public static ComplexNumber zero() { | |
return new ComplexNumber(0, 0); | |
} | |
// create a new instance via constructor | |
public ComplexNumber(float realPart, float complexPart) { | |
this.realPart = realPart; | |
this.complexPart = complexPart; | |
} | |
// create a new ComplexNumber by addition | |
public ComplexNumber add(ComplexNumber anotherComplexNumber) { | |
return new ComplexNumber( | |
realPart + anotherComplexNumber.realPart, | |
complexPart + anotherComplexNumber.complexPart | |
); | |
} | |
// extract internal data and convert to String | |
public String toString() { | |
return String.format("%f + %f i", realPart, complexPart); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment