Advertisement
rjsantiago0001

ComplexNumbers with String Constructor

Feb 29th, 2016
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1.  
  2. /*
  3.  * Ricardo Santiago
  4.  * Complex Numbers with String Constructor
  5.  * CSC-112
  6.  * 2/29/2016
  7.  */
  8.  
  9. import java.util.ArrayList;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class Complex {
  14.  
  15.     // finalize the variables to make immutable
  16.     private final double re;
  17.     private final double im;
  18.  
  19.     // Bonus string Constructor using regex
  20.     public Complex(String str) {
  21.  
  22.         ArrayList<Double> list = new ArrayList<Double>();
  23.        
  24.  
  25.        
  26.         Pattern p = Pattern.compile("[-+]?[0-9]*\\.?[0-9]");
  27.         // toString() handles the letter i
  28.  
  29.         // find positive and negative doubles in string and add to list
  30.         Matcher m = p.matcher(str);
  31.         while (m.find()) {
  32.             double num = Double.parseDouble(m.group());
  33.             list.add(num);
  34.         }
  35.         this.re = list.get(0);
  36.         this.im = list.get(1);
  37.     }
  38.  
  39.     public Complex(double real, double imag) {
  40.         this.re = real;
  41.         this.im = imag;
  42.     }
  43.  
  44.     public Complex(double real) {
  45.         this.re = real;
  46.         this.im = 0;
  47.     }
  48.  
  49.     public Complex() {
  50.         this.re = 0;
  51.         this.im = 0;
  52.     }
  53.  
  54.     public Complex add(Complex c) {
  55.         Complex a = this;
  56.         double real = a.re + c.re;
  57.         double imag = a.im + c.im;
  58.         return new Complex(real, imag);
  59.     }
  60.  
  61.     public Complex subtract(Complex c) {
  62.         Complex a = this;
  63.         return a.add(c.negate());
  64.     }
  65.  
  66.     public Complex multiply(Complex c) {
  67.         Complex a = this;
  68.         double real = (a.re * c.re) - (a.im * c.im);
  69.         double imag = (a.re * c.im) + (a.im * c.re);
  70.         return new Complex(real, imag);
  71.     }
  72.  
  73.     // I am currently looking for a faster method to calculate this, possibly
  74.     // using conjugate
  75.     public Complex divide(Complex c) {
  76.         Complex a = this;
  77.         double real = ((a.re * c.re) + (a.im * c.im)) / ((c.re * c.re) + (c.im * c.im));
  78.         double imag = ((a.im * c.re) - (a.re * c.im)) / ((c.re * c.re) + (c.im * c.im));
  79.         return new Complex(real, imag);
  80.     }
  81.  
  82.     public double abs() {
  83.         return Math.hypot(re, im);
  84.     }
  85.  
  86.     public Complex negate() {
  87.         return new Complex(-re, -im);
  88.     }
  89.  
  90.     public Complex conjugate() {
  91.         return new Complex(re, -im);
  92.     }
  93.  
  94.     public double distance(Complex c) {
  95.         return this.subtract(c).abs();
  96.  
  97.     }
  98.  
  99.     public boolean equals(Complex c) {
  100.         double diff;
  101.         if (this.greaterThan(c) == true)
  102.             diff = (this.abs() - c.abs()) / this.abs();
  103.         else
  104.             diff = (c.abs() - this.abs()) / c.abs();
  105.  
  106.         if (diff < 1E-6)
  107.             return true;
  108.         else
  109.             return false;
  110.     }
  111.  
  112.     public boolean greaterThan(Complex c) {
  113.         if (this.abs() > c.abs())
  114.             return true;
  115.         else
  116.             return false;
  117.     }
  118.  
  119.     public boolean lessThan(Complex c) {
  120.         if (this.abs() < c.abs())
  121.             return true;
  122.         else
  123.             return false;
  124.     }
  125.  
  126.     public String toString() {
  127.         if (im == 0)
  128.             return re + "";
  129.         if (re == 0)
  130.             return im + "i";
  131.         if (im < 0)
  132.             return re + " - " + (-im) + "i";
  133.         else
  134.             return re + " + " + im + "i";
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement