class Complex { // fields private double real; // real component private double imag; // imaginary component // constructor public Complex(double r, double i) { real = r; imag = i; } // methods public Complex add(Complex other) { return new Complex(real+other.real, imag+other.imag); } public String toString() { return real+"+"+imag+"i"; // what if imag is neg? } } public class TestComplex { public static void main(String[] args) { Complex c1 = new Complex(1,3); Complex c2 = new Complex(2,-1); System.out.println(c1.add(c2)); } }