Home : Course Map : Chapter 4 : Java : Tech : Improved Complex Number Class
 Introduction Inheritance   Demo 1 Overriding   Demo 2a   Demo 2b this,super MoreConstructors   Demo 3 Abstraction Interface    Demo 4 Casting References MoreAboutArrays Object class Class Summary Exercises
 About JavaTech      Codes List      Exercises      Feedback      References      Resources      Tips      Topic Index      Course Guide      What's New

In Chapter 3: Tech: Complex Number Class we created the class BasicComplex with the bare essentials needed to represent complex numbers. Here we develop a more elaborate class here.

Below we show the code listing for the Complex class (an abbreviated version of this class is shown in the book). As before, it holds the real and imaginary parts in double type instance variables. Methods like add (Complex cvalue) can modify these variables. The static methods like Complex add (Complex cvalue1, Complex cvalue2) create a new Complex object from the sum of two instances of Complex and do not affect their internal variables.

In Chapter 5: Java: Access (Visibility) Rules we discuss access modifiers like "public" and "private". In Chapter 5: Tech: An Immutable Complex Number Class we use the access modifiers to create a complex class in which the real and imaginary values can not be modified.

 Complex.java (Complex_Std follows the code style convention discussed in Chapter 5.) /**   * This is a complex number class with the essential features   * needed for computations. The real and imaginary   * parts can be accessed directly for fast operation. (See   * ComplexImm for an immutable version of a complex number   * class.)   *   * Several of the common complex number operations are provided   * as static methods. New instances of Complex are returned   * by these methods.   *  **/ public class Complex {   // Properties   public double real;   public double img;   /** Constructor that initializes the values. **/   Complex  (double r, double i) {     real = r; img = i;   }   /** Get methods for real & imaginary parts. **/   public double getReal () {     return real;   }   public double getImg () {     return img;   }   /** Define a complex add method. **/   public void add (Complex cvalue) {     real = real + cvalue.real;     img  = img  + cvalue.img;   }   /** Define a complex subtract method. **/   public void subtract (Complex cvalue) {     real = real - cvalue.real;     img  = img  - cvalue.img;   }   /**     * Define a static add method that creates a     * a new Complex object with the sum.    **/   public static Complex add (Complex cvalue1, Complex cvalue2) {     double r = cvalue1.real + cvalue2.real;     double i = cvalue1.img  + cvalue2.img;     return new Complex (r,i);   }   /** Define a static subtract method that creates a     * a new Complex object equal to     *     *  cvalue1 - cvalue2.    **/   public static Complex subtract (Complex cvalue1, Complex cvalue2) {     double r = cvalue1.real - cvalue2.real;     double i = cvalue1.img  - cvalue2.img;     return new Complex (r,i);   }   /**     * Check for the equality of this object with that of the argument.    **/   public boolean equals (Complex cvalue) {     return ( (real == cvalue.real) &&              (img  == cvalue.img) ) ;   }   /**     * Provide the magnitude of the complex value.    **/   public double modulus () {     return Math.sqrt (real*real + img*img);   }   /**     * Multiply this complex object by the complex argument.    **/   public void multiply (Complex cvalue) {     double r2 = real * cvalue.real - img * cvalue.img;     double i2 = real * cvalue.img  + img * cvalue.real;     real = r2;     img  = i2;   } // multiply   /** Define a static multiply method that creates a     * a new Complex object with the product.    **/   public static Complex multiply (Complex cvalue1, Complex cvalue2) {     double r2 = cvalue1.real * cvalue2.real -                 cvalue1.img * cvalue2.img;     double i2 = cvalue1.img  * cvalue2.real +                 cvalue1.real  * cvalue2.img;     return new Complex (r2, i2);   } // multiply   /**     * Divide this complex object by the complex argument.    **/   public void divide (Complex 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;   } // divide   /** Define a static divide method that creates a     * a new Complex object with the result of     *     *  cvalue1/cvalue2.    **/   public static Complex divide (Complex cvalue1, Complex 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 Complex (r/denom, i/denom);   } // divide   /** Return a string representation of the complex value. **/   public String toString () {     String img_sign = (img < 0) ? " - " : " + ";     return (real +  img_sign + img + "i");   } } // Complex

The add method must be explicitly invoked since, unlike C++, we cannot override the + operator and create a special + operator for complex addition. The following code shows how to add two complex numbers together.

 public class ComplexTest {   static public void main (String [] args)   {    // Create complex objects    Complex a = new Complex (1.1,2.3);    Complex b = new Complex (3.0,1.6);    Complex c = Complex.add (a,b); // c now holds a+b      ...   } }

References and Web Resources

Most recent update: June 14, 2005

 Tech MoreComplexClass ImprovedHistogram JavaRandomNums Vectors & Matrices Exercises
 Physics Runge-Kutta 2nd   Demo 1 Runge-Kutta 4th   Demo 2 BoundaryVal.Prob Shooting Method   Demo 3 Exercises
 Part I Part II Part III Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17 18 19 20 21 22 23 24 Supplements 1  2  3  4  5  6  7  8  9  10  11  12 Tech 1  2  3  4  5  6  7  8  9  10  11  12 Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.