Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ComplexRegex;
- /*
- * Lu Liu
- * CSC-112 Intermediate Java Programming
- * 03/01/2016
- * lliu0001@ student.stcc.edu
- *
- * #HW8
- * Complex Numbers Regex Application:
- * 1)test client;
- * 2)generate two Complex Number a and b;
- * 3)operate Complex Number a and b;
- * 4) one of the constructors takes a String such as "3+5i", or "17.5-21.3i", or "3.1416", or "99i";
- */
- import java.util.regex.*;
- import javax.swing.JOptionPane;
- public class ComplexRegex {
- // test client
- public static void main(String[] args) {
- final String TITLE = "ComplexNumber Demo";
- JOptionPane.showMessageDialog(null, "Running " + TITLE + "\nHave Fun!!!", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- do {
- // generate two Complex Number a and b, and test them;
- final String TITLE1 = "ComplexNumber Demo";
- String temp;
- temp = JOptionPane.showInputDialog(null, "Enter Complex Number 1: ", TITLE1,
- JOptionPane.INFORMATION_MESSAGE);
- Complex a = new Complex(temp);
- temp = JOptionPane.showInputDialog(null, "Enter Complex Number 2: ", TITLE1,
- JOptionPane.INFORMATION_MESSAGE);
- Complex b = new Complex(temp);
- Complex ans = null;
- String options[] = { "Add", "Subtract", "Multiply", "Divide", "Distance", "Compare", "negate", "conjugate",
- "abs", "Quit" };
- int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.PLAIN_MESSAGE, null, options, 4);
- String output = "";
- switch (option) {
- case 0:
- ans = a.add(b);
- output = " + ";
- break;
- case 1:
- ans = a.subtract(b);
- output = " - ";
- break;
- case 2:
- ans = a.multiply(b);
- output = " * ";
- break;
- case 3:
- ans = a.divide(b);
- output = " / ";
- break;
- default:
- break;
- }
- if (option == 4) {
- JOptionPane.showMessageDialog(null, a + " and " + b + " 's diatance = " + a.distance(b), TITLE,
- JOptionPane.PLAIN_MESSAGE);
- } else if (option == 5) {
- if (a.equals(b))
- JOptionPane.showMessageDialog(null, a + " is equals to " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- else if (a.greaterThan(b))
- JOptionPane.showMessageDialog(null, a + " is greater than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- else
- JOptionPane.showMessageDialog(null, a + " is less than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- } else if (option == 6) {
- Complex ans1 = a.negate();
- Complex ans2 = b.negate();
- JOptionPane.showMessageDialog(null,
- a + " 's negation is " + ans1 + "\n" + b + " 's negation is " + ans2, TITLE,
- JOptionPane.PLAIN_MESSAGE);
- } else if (option == 7) {
- Complex ans1 = a.conjugate();
- Complex ans2 = b.conjugate();
- JOptionPane.showMessageDialog(null,
- a + " 's conjugate is " + ans1 + "\n" + b + " 's conjugate is " + ans2, TITLE,
- JOptionPane.PLAIN_MESSAGE);
- }
- else if (option == 8) {
- double ans1 = a.abs();
- double ans2 = b.abs();
- JOptionPane.showMessageDialog(null,
- a + " 's distance is " + ans1 + "\n" + b + " 's distance is " + ans2, TITLE,
- JOptionPane.PLAIN_MESSAGE);
- } else if (option > 8)
- break;
- else {
- String sen = "(" + a + ")" + output + "(" + b + ") " + " = " + ans;
- JOptionPane.showMessageDialog(null, sen, TITLE, JOptionPane.PLAIN_MESSAGE);
- }
- // do this again?
- option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
- if (option != JOptionPane.YES_OPTION)
- break;
- } while (true);
- }
- }
- class Complex {
- // define two doubles, known as the real part and the imaginary part.
- private double real;
- private double imag;
- // define 4 constructors
- public Complex(double real, double imag) {
- this.real = real;
- this.imag = imag;
- }
- public Complex(double real) {
- this.real = real;
- this.imag = 0;
- }
- public Complex() {
- this(0, 0);
- }
- // the constructor takes a String
- public Complex(String str) {
- String p1 = "\\.*[i]$";
- // Create a Pattern object
- Pattern r1 = Pattern.compile(p1);
- // Now create matcher object.
- Matcher m = r1.matcher(str);
- if (m.find()) {
- if (str.equalsIgnoreCase("i")) {
- this.imag = 1.0;
- this.real = 0.0;
- } else if (str.equalsIgnoreCase("-i")) {
- this.imag = -1.0;
- this.real = 0.0;
- }
- String p2 = "([+-]?(\\d+(\\.\\d+)?|\\.\\d+))[i]";
- Pattern r2 = Pattern.compile(p2);
- Matcher m2 = r2.matcher(str);
- if (m2.find()) {
- this.imag = Double.parseDouble(m2.group(1));
- this.real = 0.0;
- }
- String p3 = "([+-]?(\\d+(\\.\\d+)?|\\.\\d+))(([+-]((\\d+(\\.\\d+)?|\\.\\d+))?))[i]";
- Pattern r3 = Pattern.compile(p3);
- Matcher m3 = r3.matcher(str);
- if (m3.find()) {
- if (m3.group(4).equals("-")) {
- this.real = Double.parseDouble(m3.group(1));
- this.imag = -1.0;
- } else if (m3.group(4).equals("+")) {
- this.real = Double.parseDouble(m3.group(1));
- this.imag = -1.0;
- } else {
- this.real = Double.parseDouble(m3.group(1));
- this.imag = Double.parseDouble(m3.group(4));
- }
- }
- } else {
- this.real = Double.parseDouble(str);
- this.imag = 0.0;
- }
- }
- // define public methods
- public double getReal() {
- return real;
- }
- public double getImag() {
- return imag;
- }
- public Complex add(Complex c) {
- return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
- }
- public Complex subtract(Complex c) {
- return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
- }
- public Complex multiply(Complex c) {
- return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
- (this.real * c.getImag() + c.getImag() * c.getImag()));
- }
- public Complex divide(Complex c) {
- double a = this.real;
- double b = this.imag;
- double x = c.getReal();
- double y = c.getImag();
- return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
- }
- public double abs() {
- double a = this.getReal();
- double b = this.getImag();
- return Math.sqrt(a * a + b * b);
- }
- public Complex negate() {
- double a = this.getReal();
- double b = this.getImag();
- return new Complex(-a, -b);
- }
- public Complex conjugate() {
- return new Complex(this.getReal(), -this.getImag());
- }
- public double distance(Complex c) {
- double x = this.getReal() - c.getReal();
- double y = this.getImag() - c.getImag();
- return x * x + y * y;
- }
- public boolean equals(Complex c) {
- double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
- if (minus / this.abs() < 1E-6)
- return true;
- return false;
- }
- public boolean greaterThan(Complex c) {
- return (this.abs() > c.abs()) ? true : false;
- }
- public boolean lessThan(Complex c) {
- return !this.greaterThan(c);
- }
- public String toString() {
- if (this.getImag() > 0)
- return this.getReal() + "+" + (this.getImag() + "i");
- else if (this.getImag() == 0)
- return this.getReal() + "";
- else
- return this.getReal() + "" + (this.getImag() + "i");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement