Advertisement
luliu

Complex Numbers

Feb 15th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. /*
  2.  * Lu Liu
  3.  * CSC-112 Intermediate Java Programming
  4.  * FEB 15 2016
  5.  * lliu0001@ student.stcc.edu
  6.  *
  7.  * Implementing Complex Numbers
  8.  * define two doubles, known as the real part and the imaginary part.
  9.  * define 4 constructors
  10.  * define public methods
  11.  */
  12. package chapter09;
  13.  
  14. public class Complex {
  15.  
  16.     // define two doubles, known as the real part and the imaginary part.
  17.     private double real;
  18.     private double imag;
  19.  
  20.     // define 4 constructors
  21.     public Complex(double real, double imag) {
  22.         this.real = real;
  23.         this.imag = imag;
  24.     }
  25.  
  26.     public Complex(double real) {
  27.         this.real = real;
  28.         this.imag = 0;
  29.     }
  30.  
  31.     public Complex() {
  32.         this(0, 0);
  33.     }
  34.  
  35.     // the constructor takes a String
  36.     public Complex(String str) {
  37.         String newStr = str.trim();
  38.         char[] chars = newStr.toCharArray();
  39.         if (newStr.indexOf("i") == -1)
  40.             this.real = Double.parseDouble(newStr);
  41.         else if (chars.length == 1) {
  42.             this.imag = 1.0;
  43.         } else if (chars.length == 2) {
  44.             this.imag = (double) (chars[0] - '0');
  45.         } else if (chars.length == 3) {
  46.             if (Character.isDigit(chars[0])) {
  47.                 this.real = (double) (chars[0] - '0');
  48.                 this.imag = (chars[1] == '-') ? -1.0 : 1.0;
  49.             } else
  50.                 this.imag = -(double) (chars[1] - '0');
  51.         } else if (chars.length == 4) {
  52.             if (Character.isDigit(chars[0])) {
  53.                 this.real = (double) (chars[0] - '0');
  54.                 this.imag = (chars[1] == '-') ? -(double) (chars[2] - '0') : (double) (chars[2] - '0');
  55.             } else {
  56.                 this.real = -(double) (chars[1] - '0');
  57.                 this.imag = (chars[2] == '-') ? -1.0 : 1.0;
  58.             }
  59.         } else if (chars.length == 5) {
  60.             this.real = -(double) (chars[1] - '0');
  61.             this.imag = (chars[2] == '-') ? -(double) (chars[3] - '0') : (double)(chars[3] - '0');
  62.         }
  63.     }
  64.  
  65.     // define public methods
  66.     public double getReal() {
  67.         return real;
  68.     }
  69.  
  70.     public double getImag() {
  71.         return imag;
  72.     }
  73.  
  74.     public Complex add(Complex c) {
  75.         return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
  76.     }
  77.  
  78.     public Complex subtract(Complex c) {
  79.         return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
  80.     }
  81.  
  82.     public Complex multiply(Complex c) {
  83.         return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
  84.                 (this.real * c.getImag() + c.getImag() * c.getImag()));
  85.     }
  86.  
  87.     public Complex divide(Complex c) {
  88.         double a = this.real;
  89.         double b = this.imag;
  90.         double x = c.getReal();
  91.         double y = c.getImag();
  92.         return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
  93.     }
  94.  
  95.     public double abs() {
  96.         double a = this.getReal();
  97.         double b = this.getImag();
  98.         return Math.sqrt(a * a + b * b);
  99.     }
  100.  
  101.     public Complex negate() {
  102.         double a = this.getReal();
  103.         double b = this.getImag();
  104.         return new Complex(-a, -b);
  105.     }
  106.  
  107.     public Complex conjugate() {
  108.         return new Complex(this.getReal(), -this.getImag());
  109.     }
  110.  
  111.     public double distance(Complex c) {
  112.         double x = this.getReal() - c.getReal();
  113.         double y = this.getImag() - c.getImag();
  114.         return x * x + y * y;
  115.     }
  116.  
  117.     public boolean equals(Complex c) {
  118.         double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
  119.         if (minus / this.abs() < 1E-6)
  120.             return true;
  121.         return false;
  122.     }
  123.  
  124.     public boolean greaterThan(Complex c) {
  125.         return (this.abs() > c.abs()) ? true : false;
  126.     }
  127.  
  128.     public boolean lessThan(Complex c) {
  129.         return !this.greaterThan(c);
  130.     }
  131.  
  132.     public String toString() {
  133.         if (this.getImag() > 0)
  134.             return this.getReal() + "+" + (this.getImag() + "i");
  135.         else if (this.getImag() == 0)
  136.             return this.getReal() + "";
  137.         else
  138.             return this.getReal() + "" + (this.getImag() + "i");
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement