[ Team LiB ] Previous Section Next Section

2.5 Complex Numbers

Example 2-5 shows the definition of a class that represents complex numbers. You may recall from algebra class that a complex number is the sum of a real number and an imaginary number. The imaginary number i is the square root of -1. This ComplexNumber class defines two double fields, which represent the real and imaginary parts of the number. These fields are declared private, which means they can be used only within the body of the class; they are inaccessible outside the class. Because the fields are inaccessible, the class defines two accessor methods, real( ) and imaginary( ), that simply return their values. This technique of making fields private and defining accessor methods is called encapsulation. Encapsulation hides the implementation of a class from its users, which means that you can change the implementation without it affecting the users.

Notice that the ComplexNumber class doesn't define any methods, other than the constructor, that set the values of its fields. Once a ComplexNumber object is created, the number it represents can never be changed. This property is known as immutability; it is often useful to design objects that are immutable like this.

ComplexNumber defines two add( ) methods and two multiply( ) methods that perform addition and multiplication of complex numbers. The difference between the two versions of each method is that one is an instance method and one is a class, or static, method. Consider the add( ) methods, for example. The instance method adds the value of the current instance of ComplexNumber to another specified ComplexNumber object. The class method doesn't have a current instance; it simply adds the values of two specified ComplexNumber objects. The instance method is invoked through an instance of the class, like this:

ComplexNumber sum = a.add(b);

The class method, however, is invoked through the class itself, rather than through an instance:

ComplexNumber sum = ComplexNumber.add(a, b);
Example 2-5. ComplexNumber.java
package je3.classes;

/**
 * This class represents complex numbers, and defines methods for performing
 * arithmetic on complex numbers.
 **/
public class ComplexNumber {
    // These are the instance variables.  Each ComplexNumber object holds
    // two double values, known as x and y.  They are private, so they are
    // not accessible from outside this class.  Instead, they are available 
    // through the real( ) and imaginary( ) methods below.
    private double x, y;

    /** This is the constructor.  It initializes the x and y variables */
    public ComplexNumber(double real, double imaginary) {
        this.x = real;
        this.y = imaginary;
    }

    /** 
     * An accessor method.  Returns the real part of the complex number.
     * Note that there is no setReal( ) method to set the real part.  This means
     * that the ComplexNumber class is "immutable".
     **/
    public double real( ) { return x; }
    
    /** An accessor method. Returns the imaginary part of the complex number */
    public double imaginary( ) { return y; }
    
    /** Compute the magnitude of a complex number */
    public double magnitude( ) { return Math.sqrt(x*x + y*y); }

    /** 
     * This method converts a ComplexNumber to a string.  This is a method of
     * Object that we override so that complex numbers can be meaningfully
     * converted to strings, and so they can conveniently be printed out with
     * System.out.println( ) and related methods
     **/
    public String toString( ) { return "{" + x + "," + y + "}"; }
    
    /** 
     * This is a static class method.  It takes two complex numbers, adds
     * them, and returns the result as a third number.  Because it is static,
     * there is no "current instance" or "this" object.  Use it like this:
     * ComplexNumber c = ComplexNumber.add(a, b);
     **/
    public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
        return new ComplexNumber(a.x + b.x, a.y + b.y);
    }

    /**
     * This is a non-static instance method by the same name.  It adds the
     * specified complex number to the current complex number.  Use it like
     * this:
     * ComplexNumber c = a.add(b);
     **/
    public ComplexNumber add(ComplexNumber a) {
        return new ComplexNumber(this.x + a.x, this.y+a.y);
    }

    /** A static class method to multiply complex numbers */
    public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {
        return new ComplexNumber(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
    }

    /** An instance method to multiply complex numbers */
    public ComplexNumber multiply(ComplexNumber a) {
        return new ComplexNumber(x*a.x - y*a.y, x*a.y + y*a.x);
    }
}
    [ Team LiB ] Previous Section Next Section