abdulfatirs

ComplexNumber.java | An implementation of Complex Numbers

May 15th, 2014
515
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package plane.complex;
  2.  
  3. /**
  4.  * <code>ComplexNumber</code> is a class which implements complex numbers in Java.
  5.  * It includes basic operations that can be performed on complex numbers such as,
  6.  * addition, subtraction, multiplication, conjugate, modulus and squaring.
  7.  *
  8.  * @author      Abdul Fatir
  9.  * @version     1.0
  10.  *
  11.  */
  12. public class ComplexNumber
  13. {
  14.     /**
  15.     * The real, Re(z), part of the <code>ComplexNumber</code>.
  16.     */
  17.     private double real;
  18.     /**
  19.     * The imaginary, Im(z), part of the <code>ComplexNumber</code>.
  20.     */
  21.     private double imaginary;
  22.     /**
  23.     * Constructs a new <code>ComplexNumber</code> object with both real and imaginary parts 0 (z = 0 + 0i).
  24.     */
  25.    
  26.     public ComplexNumber()
  27.     {
  28.         real = 0.0;
  29.         imaginary = 0.0;
  30.     }
  31.    
  32.     /**
  33.     * Constructs a new <code>ComplexNumber</code> object.
  34.     * @param real the real part, Re(z), of the complex number
  35.     * @param imaginary the imaginary part, Im(z), of the complex number
  36.     */
  37.    
  38.     public ComplexNumber(double real, double imaginary)
  39.     {
  40.         this.real = real;
  41.         this.imaginary = imaginary;
  42.     }
  43.    
  44.     /**
  45.     * Adds another <code>ComplexNumber</code> to the current complex number.
  46.     * @param complex_number the complex number to be added to the current complex number
  47.     */
  48.    
  49.     public void add(ComplexNumber complex_number)
  50.     {
  51.         this.real = this.real + complex_number.real;
  52.         this.imaginary = this.imaginary + complex_number.imaginary;
  53.     }
  54.    
  55.     /**
  56.     * The complex conjugate of the current complex number.
  57.     * @return a <code>ComplexNumber</code> object which is the conjugate of the current complex number
  58.     */
  59.    
  60.     public ComplexNumber conjugate()
  61.     {
  62.         return new ComplexNumber(this.real,-this.imaginary);
  63.     }
  64.    
  65.     /**
  66.     * The modulus, magnitude or the absolute value of current complex number.
  67.     * @return the magnitude or modulus of current complex number
  68.     */
  69.    
  70.     public double mod()
  71.     {
  72.         return Math.sqrt(Math.pow(this.real,2) + Math.pow(this.imaginary,2));
  73.     }
  74.    
  75.     /**
  76.     * The square of the current complex number.
  77.     * @return a <code>ComplexNumber</code> object which is the square of the current complex number
  78.     */
  79.    
  80.     public ComplexNumber square()
  81.     {
  82.         double _real = this.real*this.real - this.imaginary*this.imaginary;
  83.         double _imaginary = 2*this.real*this.imaginary;
  84.         return new ComplexNumber(_real,_imaginary);
  85.     }
  86.    
  87.     /**
  88.     * Multiplies another <code>ComplexNumber</code> to the current complex number.
  89.     * @param complex_number the complex number to be multiplied to the current complex number
  90.     */
  91.    
  92.     public void multiply(ComplexNumber complex_number)
  93.     {
  94.         double _real = this.real*complex_number.real - this.imaginary*complex_number.imaginary;
  95.         double _imaginary = this.real*complex_number.imaginary + this.imaginary*complex_number.real;
  96.        
  97.         this.real = _real;
  98.         this.imaginary = _imaginary;
  99.     }
  100.    
  101.     /**
  102.     * Prints the complex number in x + yi format
  103.     */
  104.    
  105.     public void print()
  106.     {
  107.         System.out.println(this.real+" + "+this.imaginary+"i");
  108.     }
  109. }
RAW Paste Data