Skip to content

Instantly share code, notes, and snippets.

/ComplexNumber.java
Created Jan 26, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.