Omar_Natour

Natour, O. 2/19/16 Csc-112 Complex numbers

Feb 19th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 2/19/16
  4.  * Csc-112
  5.  * Intro to Java
  6.  * Implementing Complex Numbers p1
  7.  * Implement support for imaginary numbers through the use of objects and classes.
  8.  */
  9.  
  10. public class Complex { // decleration of doubles and constructors.
  11.  
  12.     private double real;
  13.     private double imag;
  14.  
  15.     public Complex(double r, double i) {
  16.         this.real = r;
  17.         this.imag = i;
  18.     }
  19.  
  20.     public Complex(double r) {
  21.         this.real = r;
  22.         this.imag = 0;
  23.     }
  24.  
  25.     public Complex() {
  26.         this.real = 0;
  27.         this.imag = 0;
  28.     }
  29.    
  30.     //this is where the regex constructor would go... I guess that means no java parties for me.
  31.  
  32.     /////////////////////////////////////////////////////////////////////////////////////////
  33.     // public methods to manipulate the complex numbers.
  34.  
  35.     public Complex add(Complex c) {
  36.         double r = this.real + c.real;
  37.         double i = this.imag + c.imag;
  38.        
  39.         return new Complex(r, i);
  40.     }
  41.  
  42.     public Complex subtract(Complex c) {
  43.         double r = this.real - c.real;
  44.         double i = this.imag - c.imag;
  45.        
  46.         return new Complex(r, i);
  47.     }
  48.  
  49.     public Complex multiply(Complex c) {
  50.         double r = (this.real * c.real) - (this.imag * c.imag);
  51.         double i = (this.real * c.imag) + (this.imag * c.real);
  52.  
  53.         return new Complex(r, i);
  54.     }
  55.  
  56.     public Complex divide(Complex c) {
  57.         double r0 = (this.real * c.real) + (this.imag * c.imag);
  58.         double r1 = ((c.real * c.real) + (c.imag * c.imag));
  59.         double i0 = (this.imag * c.real) - (this.real * c.imag);
  60.         double i1 = ((c.real * c.real) + (c.imag * c.imag));
  61.  
  62.         double r = r0 / r1;
  63.         double i = i0 / i1;
  64.        
  65.         return new Complex(r, i);
  66.     }
  67.  
  68.     public double abs() {
  69.         return (Math.sqrt((this.real * this.real) + (this.imag * this.imag)));
  70.     }
  71.  
  72.     public Complex negate() {
  73.         double r = -1 * (this.real);
  74.         double i = -1 * (this.imag);
  75.        
  76.         return new Complex(r, i);
  77.     }
  78.  
  79.     public Complex conjugate() {
  80.         return new Complex(this.real, -this.imag);
  81.     }
  82.  
  83.     public double distance(Complex c) {
  84.         return Math.sqrt(Math.pow(this.real - c.real, 2) + Math.pow(this.imag - c.imag, 2));
  85.     }
  86.  
  87.     public boolean equals(Complex c) {
  88.         double e;
  89.         double d = this.distance(c);
  90.         double abs1 = this.abs();
  91.         double abs2 = c.abs();
  92.  
  93.         if (abs1 > abs2)
  94.             e = d / abs1;
  95.         else
  96.             e = d / abs2;
  97.        
  98.         if (e < .000001)
  99.             return true;
  100.         return false;
  101.     }
  102.  
  103.     public boolean greaterThan(Complex c) {
  104.         double d0 = this.abs();
  105.         double d1 = c.abs();
  106.  
  107.         if (d0 > d1)
  108.             return true;
  109.         return false;
  110.     }
  111.  
  112.     public boolean lessThan(Complex c) {
  113.         double d0 = this.abs();
  114.         double d1 = c.abs();
  115.  
  116.         if (d0 < d1)
  117.             return true;
  118.         return false;
  119.     }
  120.  
  121.     public String toString(Complex c) {
  122.         double r = this.real;
  123.         double i = this.imag;
  124.  
  125.         return (Double.toString(r) + " + " + Double.toString(i) + "i");
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment