FileDocCategorySizeDatePackage
Complex.javaAPI DocExample1844Tue May 11 23:40:30 BST 1999None

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.1 1999/05/12 02:40:31 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 Complexs

		return new Complex(c1.r+c2.r, c1.i+c2.i);
	
public doublegetImaginary()
Return just the Real part

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

		return r;
	
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 Complexs

		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 Complexs

		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();