// "Java Tech" // Code provided with book for educational purposes only. // No warranty or guarantee implied. // This code freely available. No copyright claimed. // 2003 // // /** * This is a kthComplex number class with the essential features * needed for computations. The real and imaginary * parts can be accessed directly for fast operation. (See * kthComplexImm for an immutable version of a kthComplex number * class.) * * Several of the common kthComplex number operations are provided * as static methods. New instances of kthComplex are returned * by the operations. * **/ public class kthComplex { // Properties public double real; public double img; /** Constructor that initializes the real & imag values. **/ kthComplex (double r, double i) { real = r; img = i; } /** Get methods for real & imaginary parts. **/ public double real () { return real; } public double img () { return img; } /** Define an add method. **/ public void add (kthComplex cvalue) { real = real + cvalue.real; img = img + cvalue.img; } /** Define a subtract method. **/ public void subtract (kthComplex cvalue) { real = real - cvalue.real; img = img - cvalue.img; } /** * Define a static add method that creates a * a new kthComplex object with the sum. **/ public static kthComplex add (kthComplex cvalue1, kthComplex cvalue2) { double r = cvalue1.real + cvalue2.real; double i = cvalue1.img + cvalue2.img; return new kthComplex (r,i); } /** Define a static subtract method that creates a * a new kthComplex object equal to * * cvalue1 - cvalue2. **/ public static kthComplex subtract (kthComplex cvalue1, kthComplex cvalue2) { double r = cvalue1.real - cvalue2.real; double i = cvalue1.img - cvalue2.img; return new kthComplex (r,i); } /** * Check for the equality of this object with that of the argument. **/ public boolean equals (kthComplex cvalue) { return ( (real == cvalue.real) && (img == cvalue.img) ) ; } /** * Provide the magnitude of the kthComplex value. **/ public double modulus () { return Math.sqrt (real*real + img*img); } /** * Multiply this kthComplex object by the kthComplex argument. **/ public void multiply (kthComplex cvalue) { double r2 = real * cvalue.real - img * cvalue.img; double i2 = real * cvalue.img + img * cvalue.real; real = r2; img = i2; } /** Define a static multiply method that creates a * a new kthComplex object with the product. **/ public static kthComplex multiply (kthComplex cvalue1, kthComplex cvalue2) { double r2 = cvalue1.real * cvalue2.real - cvalue1.img * cvalue2.img; double i2 = cvalue1.img * cvalue2.real + cvalue1.real * cvalue2.img; return new kthComplex (r2, i2); } /** * Divide this kthComplex object by the kthComplex argument. **/ public void divide (kthComplex cvalue) { double denom = cvalue.real * cvalue.real + cvalue.img * cvalue.img; double r = real * cvalue.real + img * cvalue.img; double i = img * cvalue.real - real * cvalue.img; real = r/denom; img = i/denom; } /** Define a static divide method that creates a * a new kthComplex object with the result of * * cvalue1/cvalue2. **/ public static kthComplex divide (kthComplex cvalue1, kthComplex cvalue2) { double denom = cvalue2.real * cvalue2.real + cvalue2.img * cvalue2.img; double r = cvalue1.real * cvalue2.real + cvalue1.img * cvalue2.img; double i = cvalue1.img * cvalue2.real - cvalue1.real * cvalue2.img; return new kthComplex (r/denom, i/denom); } /** Return a string representation of the kthComplex value. **/ public String toString () { String img_sign = (img < 0) ? " - " : " + "; return (real + img_sign + img + "i"); } } // Complex