FileDocCategorySizeDatePackage
Complex.javaAPI DocExample1184Wed Apr 20 15:16:58 BST 2005None

Complex.java

/**
 * This immutable class represents <i>complex numbers</i>. 
 *
 * @author David Flanagan
 * @version 1.0
 */
public class Complex {
    /**
     * Holds the real part of this complex number. 
     * @see #y
     */
    protected double x;

    /**
     * Holds the imaginary part of this complex number. 
     * @see #x
     */
    protected double y;

    /**
     * Creates a new Complex object that represents the complex number x+yi. 
     * @param x The real part of the complex number. 
     * @param y The imaginary part of the complex number. 
     */
    public Complex(double x, double y) {
        this.x = x;
        this.y = y;
    }

    /**
     * Adds two Complex objects and produces a third object that represents
     * their sum. 
     * @param c1 A Complex object
     * @param c2 Another Complex object
     * @return  A new Complex object that represents the sum of 
     *          <code>c1</code> and <code>c2</code>. 
     * @exception java.lang.NullPointerException 
     *            If either argument is <code>null</code>. 
     */
    public static Complex add(Complex c1, Complex c2) {
        return new Complex(c1.x + c2.x, c1.y + c2.y);
    }
}