Advertisement
Omar_Natour

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

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