Advertisement
luliu

Complex Numbers Regex Application

Mar 1st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. package ComplexRegex;
  2. /*
  3.  * Lu Liu
  4.  * CSC-112 Intermediate Java Programming
  5.  * 03/01/2016
  6.  * lliu0001@ student.stcc.edu
  7.  *
  8.  * #HW8
  9.  * Complex Numbers Regex Application:
  10.  * 1)test client;
  11.  * 2)generate two Complex Number a and b;
  12.  * 3)operate Complex Number a and b;
  13.  * 4) one of the constructors takes a String such as "3+5i", or "17.5-21.3i", or "3.1416", or "99i";
  14.  */
  15.  
  16. import java.util.regex.*;
  17. import javax.swing.JOptionPane;
  18.  
  19. public class ComplexRegex {
  20.  
  21.     // test client
  22.     public static void main(String[] args) {
  23.         final String TITLE = "ComplexNumber Demo";
  24.         JOptionPane.showMessageDialog(null, "Running " + TITLE + "\nHave Fun!!!", TITLE,
  25.                 JOptionPane.INFORMATION_MESSAGE);
  26.  
  27.         do {
  28.             // generate two Complex Number a and b, and test them;
  29.             final String TITLE1 = "ComplexNumber Demo";
  30.             String temp;
  31.  
  32.             temp = JOptionPane.showInputDialog(null, "Enter Complex Number 1: ", TITLE1,
  33.                     JOptionPane.INFORMATION_MESSAGE);
  34.             Complex a = new Complex(temp);
  35.  
  36.             temp = JOptionPane.showInputDialog(null, "Enter Complex Number 2: ", TITLE1,
  37.                     JOptionPane.INFORMATION_MESSAGE);
  38.             Complex b = new Complex(temp);
  39.  
  40.             Complex ans = null;
  41.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Distance", "Compare", "negate", "conjugate",
  42.                     "abs", "Quit" };
  43.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  44.                     JOptionPane.PLAIN_MESSAGE, null, options, 4);
  45.             String output = "";
  46.             switch (option) {
  47.             case 0:
  48.                 ans = a.add(b);
  49.                 output = " + ";
  50.                 break;
  51.             case 1:
  52.                 ans = a.subtract(b);
  53.                 output = " - ";
  54.                 break;
  55.             case 2:
  56.                 ans = a.multiply(b);
  57.                 output = " * ";
  58.                 break;
  59.             case 3:
  60.                 ans = a.divide(b);
  61.                 output = " / ";
  62.                 break;
  63.             default:
  64.                 break;
  65.             }
  66.             if (option == 4) {
  67.                 JOptionPane.showMessageDialog(null, a + " and " + b + " 's diatance = " + a.distance(b), TITLE,
  68.                         JOptionPane.PLAIN_MESSAGE);
  69.             } else if (option == 5) {
  70.                 if (a.equals(b))
  71.                     JOptionPane.showMessageDialog(null, a + " is equals to " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  72.                 else if (a.greaterThan(b))
  73.                     JOptionPane.showMessageDialog(null, a + " is greater than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  74.                 else
  75.                     JOptionPane.showMessageDialog(null, a + " is less than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  76.  
  77.             } else if (option == 6) {
  78.                 Complex ans1 = a.negate();
  79.                 Complex ans2 = b.negate();
  80.                 JOptionPane.showMessageDialog(null,
  81.                         a + " 's negation is " + ans1 + "\n" + b + " 's negation is " + ans2, TITLE,
  82.                         JOptionPane.PLAIN_MESSAGE);
  83.             } else if (option == 7) {
  84.                 Complex ans1 = a.conjugate();
  85.                 Complex ans2 = b.conjugate();
  86.                 JOptionPane.showMessageDialog(null,
  87.                         a + " 's conjugate is " + ans1 + "\n" + b + " 's conjugate is " + ans2, TITLE,
  88.                         JOptionPane.PLAIN_MESSAGE);
  89.             }
  90.  
  91.             else if (option == 8) {
  92.                 double ans1 = a.abs();
  93.                 double ans2 = b.abs();
  94.                 JOptionPane.showMessageDialog(null,
  95.                         a + " 's distance is " + ans1 + "\n" + b + " 's distance is " + ans2, TITLE,
  96.                         JOptionPane.PLAIN_MESSAGE);
  97.             } else if (option > 8)
  98.                 break;
  99.             else {
  100.                 String sen = "(" + a + ")" + output + "(" + b + ") " + " = " + ans;
  101.                 JOptionPane.showMessageDialog(null, sen, TITLE, JOptionPane.PLAIN_MESSAGE);
  102.             }
  103.  
  104.             // do this again?
  105.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
  106.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  107.             if (option != JOptionPane.YES_OPTION)
  108.                 break;
  109.         } while (true);
  110.     }
  111.  
  112. }
  113.  
  114. class Complex {
  115.  
  116.     // define two doubles, known as the real part and the imaginary part.
  117.     private double real;
  118.     private double imag;
  119.  
  120.     // define 4 constructors
  121.     public Complex(double real, double imag) {
  122.         this.real = real;
  123.         this.imag = imag;
  124.     }
  125.  
  126.     public Complex(double real) {
  127.         this.real = real;
  128.         this.imag = 0;
  129.     }
  130.  
  131.     public Complex() {
  132.         this(0, 0);
  133.     }
  134.  
  135.     // the constructor takes a String
  136.     public Complex(String str) {
  137.         String p1 = "\\.*[i]$";
  138.         // Create a Pattern object
  139.         Pattern r1 = Pattern.compile(p1);
  140.         // Now create matcher object.
  141.         Matcher m = r1.matcher(str);
  142.         if (m.find()) {
  143.             if (str.equalsIgnoreCase("i")) {
  144.                 this.imag = 1.0;
  145.                 this.real = 0.0;
  146.             } else if (str.equalsIgnoreCase("-i")) {
  147.                 this.imag = -1.0;
  148.                 this.real = 0.0;
  149.             }
  150.  
  151.             String p2 = "([+-]?(\\d+(\\.\\d+)?|\\.\\d+))[i]";
  152.             Pattern r2 = Pattern.compile(p2);
  153.             Matcher m2 = r2.matcher(str);
  154.             if (m2.find()) {
  155.                 this.imag = Double.parseDouble(m2.group(1));
  156.                 this.real = 0.0;
  157.             }
  158.  
  159.             String p3 = "([+-]?(\\d+(\\.\\d+)?|\\.\\d+))(([+-]((\\d+(\\.\\d+)?|\\.\\d+))?))[i]";
  160.             Pattern r3 = Pattern.compile(p3);
  161.             Matcher m3 = r3.matcher(str);
  162.             if (m3.find()) {
  163.                 if (m3.group(4).equals("-")) {
  164.                     this.real = Double.parseDouble(m3.group(1));
  165.                     this.imag = -1.0;
  166.                 } else if (m3.group(4).equals("+")) {
  167.                     this.real = Double.parseDouble(m3.group(1));
  168.                     this.imag = -1.0;
  169.                 } else {
  170.                     this.real = Double.parseDouble(m3.group(1));
  171.                     this.imag = Double.parseDouble(m3.group(4));
  172.                 }
  173.             }
  174.  
  175.         } else {
  176.             this.real = Double.parseDouble(str);
  177.             this.imag = 0.0;
  178.         }
  179.     }
  180.  
  181.     // define public methods
  182.     public double getReal() {
  183.         return real;
  184.     }
  185.  
  186.     public double getImag() {
  187.         return imag;
  188.     }
  189.  
  190.     public Complex add(Complex c) {
  191.         return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
  192.     }
  193.  
  194.     public Complex subtract(Complex c) {
  195.         return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
  196.     }
  197.  
  198.     public Complex multiply(Complex c) {
  199.         return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
  200.                 (this.real * c.getImag() + c.getImag() * c.getImag()));
  201.     }
  202.  
  203.     public Complex divide(Complex c) {
  204.         double a = this.real;
  205.         double b = this.imag;
  206.         double x = c.getReal();
  207.         double y = c.getImag();
  208.         return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
  209.     }
  210.  
  211.     public double abs() {
  212.         double a = this.getReal();
  213.         double b = this.getImag();
  214.         return Math.sqrt(a * a + b * b);
  215.     }
  216.  
  217.     public Complex negate() {
  218.         double a = this.getReal();
  219.         double b = this.getImag();
  220.         return new Complex(-a, -b);
  221.     }
  222.  
  223.     public Complex conjugate() {
  224.         return new Complex(this.getReal(), -this.getImag());
  225.     }
  226.  
  227.     public double distance(Complex c) {
  228.         double x = this.getReal() - c.getReal();
  229.         double y = this.getImag() - c.getImag();
  230.         return x * x + y * y;
  231.     }
  232.  
  233.     public boolean equals(Complex c) {
  234.         double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
  235.         if (minus / this.abs() < 1E-6)
  236.             return true;
  237.         return false;
  238.     }
  239.  
  240.     public boolean greaterThan(Complex c) {
  241.         return (this.abs() > c.abs()) ? true : false;
  242.     }
  243.  
  244.     public boolean lessThan(Complex c) {
  245.         return !this.greaterThan(c);
  246.     }
  247.  
  248.     public String toString() {
  249.         if (this.getImag() > 0)
  250.             return this.getReal() + "+" + (this.getImag() + "i");
  251.         else if (this.getImag() == 0)
  252.             return this.getReal() + "";
  253.         else
  254.             return this.getReal() + "" + (this.getImag() + "i");
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement