A Complex Class The following class encapsulates a complex number: public class Complex { private double re; // real part private double im; // imaginary part public Complex() // default constructor { re = im = 0; } public Complex(double a, double b) // a + bi { re = a; im = b; } Complex add( Complex z) { //(a + bi) + (c + di) = (a + b) + (c + d)i. Complex sum = new Complex(); sum.re = re + z.re; sum.im = im + z.im; return sum; } Complex sub( Complex z) { //(a + bi) - (c + di) = (a - b) + (c - d)i. Complex difference = new Complex(); difference.re = re - z.re; difference.im = im - z.im; return difference; } Complex mul( Complex z) { // (a + bi) *(c + di) = (ac – bd) + (cb – ad)i Complex product = new Complex(); product.re = re * z.re - im * z.im; // (ac – bd) product.im = re * z.im + im * z.re; // (cb – ad) return product; } double abs() { return Math.sqrt(re*re + im*im); } double real() { return re; } double imaginary() { return im; } } Complex Functions Just as the function h(x) = x2,, where x is a real number, pairs a real number, x, with its square, the complex valued function f(z) = z2 pairs a complex number z with its square. For example, f(i) = i2 = -1 f(2 + 3i) = (2+3i)*(2+3i) = (4 – 9) + (6 + 6) i = -5 + 12i f(3 + 7i) = (3 + 7i)(3 + 7i) = -40 + 42i Similarly, if f(z) = z2 + (3+ 2i) , then f(i) = i2 + (3 + 2i) = -1 + (3 + 2i) = 2 + 2i f(2+3i) = (2+3i)(2+3i) + (3 + 2i) = (-5 + 12i) + (3 + 2i) = -2 + 14i For example, a complex function such as f(z) = z2 and f(z) = z2 + (3+ 2i) can be implemented as: public class ComplexFunctions { public static Complex f(Complex z) { // f(z) = z* z return z.mul(z); } public static Complex g(Complex z) { // f(z) = z*z + (3 + 2i) Complex constant = new Complex(3,2); // 3 + 2i return (z.mul(z)).add(constant); // z*z + (3+2i) } }