Loading...
MAIN MENU
ABOUT
MOODBOARD
LIBRARY
DIARY
 
            public ComplexNumber(double real, double imaginary) {
                this.real = real;
                this.imaginary = imaginary;
            }
    
            public ComplexNumber add(ComplexNumber other) {
                return new ComplexNumber(this.real + other.real, this.imaginary + other.imaginary);
            }
    
            public ComplexNumber subtract(ComplexNumber other) {
                return new ComplexNumber(this.real - other.real, this.imaginary - other.imaginary);
            }
    
            public ComplexNumber multiply(ComplexNumber other) {
                double newReal = (this.real * other.real) - (this.imaginary * other.imaginary);
                double newImaginary = (this.real * other.imaginary) + (this.imaginary * other.real);
                return new ComplexNumber(newReal, newImaginary);
            }
    
            public String toString() {
                if (imaginary == 0) return real + "";
                if (real == 0) return imaginary + "i";
                if (imaginary < 0) return real + " - " + (-imaginary) + "i";
                return real + " + " + imaginary + "i";
            }