Numbers
Class ComplexNumber

java.lang.Object
  extended by Numbers.ComplexNumber

public class ComplexNumber
extends java.lang.Object

Models a complex number, in the form "a + bi".

Author:
Chris Hamner

Field Summary
private  double imag
          imaginary part of the complex number
private  double real
          real part of the complex number
 
Constructor Summary
ComplexNumber(ComplexNumber cn)
          Constructs a complex number based on another complex number.
ComplexNumber(double a, double b)
          Constructs a new complex number.
 
Method Summary
 ComplexNumber add(ComplexNumber cn1)
          Adds 2 complex numbers.
 ComplexNumber divide(ComplexNumber cn1)
          Divides 2 complex numbers.
 double getImag()
          Returns the imaginary part of the complex number.
 double getReal()
          Returns the real part of the complex number.
 ComplexNumber multiply(ComplexNumber cn1)
          Multiplies 2 complex numbers.
 void multiply(double x)
          Multiplies a complex number by a regular number.
 void setImag(double a)
          Sets the imaginary part of the complex number.
 void setReal(double a)
          Sets the real part of the complex number.
 void squareRoot()
          Takes the square root of a complex number.
 ComplexNumber subtract(ComplexNumber cn1)
          Subtracts 2 complex numbers.
 java.lang.String toString()
          A string which returns the real and imaginary parts of the complex number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

real

private double real
real part of the complex number


imag

private double imag
imaginary part of the complex number

Constructor Detail

ComplexNumber

public ComplexNumber(double a,
                     double b)
Constructs a new complex number.

Parameters:
a - - The real part of the complex number.
b - - The imaginary part of the complex number.

ComplexNumber

public ComplexNumber(ComplexNumber cn)
Constructs a complex number based on another complex number.

Parameters:
cn - - The other complex number.
Method Detail

setReal

public void setReal(double a)
Sets the real part of the complex number.

Parameters:
a - - value for real part.

getReal

public double getReal()
Returns the real part of the complex number.


setImag

public void setImag(double a)
Sets the imaginary part of the complex number.

Parameters:
a - - value for imaginary part.

getImag

public double getImag()
Returns the imaginary part of the complex number.


multiply

public ComplexNumber multiply(ComplexNumber cn1)
Multiplies 2 complex numbers.

Parameters:
cn1 - - the second complex number.
Returns:
- the solution.

divide

public ComplexNumber divide(ComplexNumber cn1)
Divides 2 complex numbers.

Parameters:
cn1 - - the second complex number.
Returns:
- the solution.

subtract

public ComplexNumber subtract(ComplexNumber cn1)
Subtracts 2 complex numbers.

Parameters:
cn1 - - the second complex number.
Returns:
- the solution.

add

public ComplexNumber add(ComplexNumber cn1)
Adds 2 complex numbers.

Parameters:
cn1 - - the second complex number.
Returns:
- the solution.

multiply

public void multiply(double x)
Multiplies a complex number by a regular number.

Parameters:
x - - regular number by which the complex number will be multiplied.

squareRoot

public void squareRoot()
Takes the square root of a complex number. Tom wrote this method, I did not. Thank you Tom. All credit goes to Tom. Did I mention that Tom wrote this method? Thanks Tom.


toString

public java.lang.String toString()
A string which returns the real and imaginary parts of the complex number.

Overrides:
toString in class java.lang.Object