FileDocCategorySizeDatePackage
Complex.javaAPI DocExample2509Thu May 13 18:29:00 BST 2004None

Complex

public class Complex extends Object
A class to represent Complex Numbers. A Complex object is immutable once created; the add, subtract and multiply routines return newly-created Complex objects containing the results.
author
Ian F. Darwin, inspired by David Flanagan.
version
$Id: Complex.java,v 1.3 2004/05/13 22:28:59 ian Exp $

Fields Summary
private double
r
The real part
private double
i
The imaginary part
Constructors Summary
Complex(double rr, double ii)
Construct a Complex

		r = rr;
		i = ii;
	
Methods Summary
public Complexadd(Complex other)
Add another Complex to this one

		return add(this, other);
	
public static Complexadd(Complex c1, Complex c2)
Add two Complexes

		return new Complex(c1.r+c2.r, c1.i+c2.i);
	
public static Complexdivide(Complex c1, Complex c2)
Divide c1 by c2.

author
Gisbert Selke.

		return new Complex(
			(c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i),
			(c1.i*c2.r-c1.r*c2.i)/(c2.r*c2.r+c2.i*c2.i));
	
public booleanequals(java.lang.Object o)

		if (!(o instanceof Complex))
			throw new IllegalArgumentException(
					"Complex.equals argument must be a Complex");
		Complex other = (Complex)o;
		return r == other.r && i == other.i;
	
public doublegetImaginary()
Return just the Real part

		return i;
	
public doublegetReal()
Return just the Real part

		return r;
	
public inthashCode()

		return (int)( r) |  (int)i;
	
public doublemagnitude()
Return the magnitude of a complex number

		return Math.sqrt(r*r + i*i);
	
public Complexmultiply(Complex other)
Multiply this Complex times another one

		return multiply(this, other);
	
public static Complexmultiply(Complex c1, Complex c2)
Multiply two Complexes

		return new Complex(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r);
	
public Complexsubtract(Complex other)
Subtract another Complex from this one

		return subtract(this, other);
	
public static Complexsubtract(Complex c1, Complex c2)
Subtract two Complexes

		return new Complex(c1.r-c2.r, c1.i-c2.i);
	
public java.lang.StringtoString()
Display the current Complex as a String, for use in println() and elsewhere.

		StringBuffer sb = new StringBuffer().append(r);
		if (i>0)
			sb.append('+");	// else append(i) appends - sign
		return sb.append(i).append('i").toString();