// Bob Myers // class Complex -- for creating complex number objects // // in-class example for CIS 3931 class Complex { private double real = 0; private double imaginary = 0; public Complex() { } public Complex(double r) { this(r,0); } public Complex(double r, double i) { real = r; imaginary = i; } public Complex add(Complex c) { return new Complex(real + c.real, imaginary + c.imaginary); } public Complex subtract(Complex c) { Complex result = new Complex(); result.real = real - c.real; result.imaginary = imaginary - c.imaginary; return result; } public Complex multiply(Complex c) { Complex result = new Complex(); result.real = (real * c.real - imaginary * c.imaginary); result.imaginary = (real * c.imaginary + imaginary * c.real); return result; } public String toString() { return (real + " + " + imaginary + "i"); } public static void main(String[] args) { Complex c1 = new Complex(); Complex c2 = new Complex(5); Complex c3 = new Complex(3, 4); Complex c4 = new Complex(10, 5); System.out.println("c1 = " + c1.toString()); System.out.println("c2 = " + c2); System.out.println("c2 = " + c3); System.out.println("c2 = " + c4); System.out.println("c1.add(c2) = " + c1.add(c2)); System.out.println("c3.add(c4) = " + c3.add(c4)); System.out.println("c2.subtract(c1) = " + c2.subtract(c1)); System.out.println("c4.subtract(c3) = " + c4.subtract(c3)); } }