import java.lang.Math; public class Complex { private double real = 0.0; private double img = 0.0; public Complex(){} public Complex(double r, double i) { real = r; img = i; } //alternate constructor for ints public Complex(int r, int i) { real = (double)r; img = (double)i; } public double getReal() { return real; } public double getImg() { return img; } public void add(Complex cvalue) { real = real + cvalue.real; img = img + cvalue.img; } public void subtract(Complex cvalue) { real = real - cvalue.real; img = img - cvalue.img; } public double distanceToOrigin() { return Math.sqrt((real * real) + (img * img)); } public static Complex add(Complex cvalue1, Complex cvalue2) { double r = cvalue1.getReal() + cvalue2.getReal(); double i = cvalue1.getImg() + cvalue2.getImg(); return new Complex(r,i); } public static Complex subtract(Complex cvalue1, Complex cvalue2) { double r = cvalue1.getReal() - cvalue2.getReal(); double i = cvalue1.getImg() - cvalue2.getImg(); return new Complex (r,i); } public static Complex multiply(Complex cvalue1, Complex cvalue2) { double a = cvalue1.getReal(); double b = cvalue1.getImg(); double c = cvalue2.getReal(); double d = cvalue2.getImg(); double realPart = (a * c) - (b * d); double imagPart = (a * d) + (b * c); return new Complex(realPart, imagPart); } public String toString() { return real + "+" + img + "i"; } }