Advertisement
Guest User

Complex Number Class

a guest
Feb 25th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. /* Name: Christopher Watkins
  2.  * Date: 2/23/17
  3.  * Course Number: CSC-112
  4.  * Course Name: Intermediate Java
  5.  * Problem Number: Complex number Class
  6.  * Email: cmwatkins@student.stcc.edu
  7.  * Implement a basic idea of complex numbers in Java
  8. */
  9.  
  10. public class Complex {
  11.    
  12.     private double real = 0;
  13.     private double imag = 0;
  14.  
  15.     /** Operations */
  16.    
  17.     public Complex add(Complex c){
  18.         return new Complex(this.real+c.real,this.imag+c.imag);
  19.     }
  20.    
  21.     public Complex subtract(Complex c){
  22.         return new Complex(this.real-c.real,this.imag-c.imag);
  23.     }
  24.    
  25.     public Complex multiply(Complex c){
  26.         return new Complex((c.real * this.real) - (c.imag * this.imag),(c.real * this.imag) + (this.real * c.imag));
  27.     }
  28.    
  29.     public Complex divide(Complex c){
  30.         /** Removing 'i' from the denominator */
  31.        
  32.         Complex numerator = new Complex((this.real*c.real)+(this.imag*c.imag),(c.real * this.imag) + (this.real * c.imag));
  33.         double denom = (c.real*c.real) + (c.imag*c.imag);
  34.        
  35.         /** The actual Division */
  36.         return new Complex(numerator.real/denom,numerator.imag/denom);
  37.     }
  38.    
  39.     public double abs(){
  40.         return Math.sqrt(Math.pow(this.real,2)+Math.pow(this.imag,2));
  41.     }
  42.    
  43.     public Complex negate(){
  44.         return new Complex(this.real * -1,this.imag * -1);
  45.     }
  46.    
  47.     public Complex conjugate(){
  48.         return new Complex(this.real,this.imag * -1);
  49.  
  50.     }
  51.    
  52.     public double distance(Complex c){
  53.         return Math.sqrt(Math.pow(this.real - c.real,2) + Math.pow(this.imag - c.imag,2));
  54.     }
  55.    
  56.     public boolean equals(Complex c){
  57.         if(distance(this) == distance(c))
  58.             return true;
  59.         return false;
  60.     }
  61.    
  62.     public boolean greaterThan(Complex c){
  63.         Complex origin = new Complex();
  64.         // Checks distance between each Complex number and 0,0 then compares
  65.         if(Math.sqrt(Math.pow(this.real - origin.real,2) + Math.pow(this.imag - origin.imag,2)) > Math.sqrt(Math.pow(origin.real - c.real,2) + Math.pow(origin.imag - c.imag,2)))
  66.             return true;
  67.         return false;
  68.     }
  69.    
  70.     public boolean lessThan(Complex c){
  71.         Complex origin = new Complex();
  72.         // Checks distance between each Complex number and 0,0 then compares
  73.         if(Math.sqrt(Math.pow(this.real - origin.real,2) + Math.pow(this.imag - origin.imag,2)) < Math.sqrt(Math.pow(origin.real - c.real,2) + Math.pow(origin.imag - c.imag,2)))
  74.             return true;
  75.         return false;
  76.     }
  77.    
  78.     /** Basic Constructors */
  79.    
  80.     public Complex(){
  81.         this(0);
  82.     }
  83.    
  84.     public Complex(double real){
  85.         this(real,0);
  86.     }
  87.    
  88.     public Complex(double real, double imag){
  89.         this.real = real;
  90.         this.imag = imag;
  91.     }
  92.    
  93.     /** Print Out */
  94.    
  95.     @Override
  96.     public String toString() {
  97.         return this.real + " + " + this.imag + " i";
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement