Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 3/1/16
- * Csc-112
- * Intro to Java
- * Implementing Complex Numbers p3
- * Ojnatour0001@student.stcc.edu or natour@cs.stcc.edu
- * Implement support for imaginary numbers through the use of objects and classes and regex.
- */
- import javax.swing.JOptionPane;
- import java.util.regex.*;
- public class ComplexNumberDriver {
- public static void main(String[] args) {
- final String TITLE = "Complex Numbers";
- double real = 0;
- double imag = 0;
- double real2 = 0;
- double imag2 = 0;
- String output = "";
- while (true) {
- String a = JOptionPane.showInputDialog(null, "Enter the first complex number", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- String b = JOptionPane.showInputDialog(null, "Enter the second complex number", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- Pattern p = Pattern.compile(
- ("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+) *(i)?)( *(([\\+\\-]) *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i *)?"));
- Matcher match = p.matcher(a);
- Matcher match1 = p.matcher(b);
- while (match.find()) {
- String mat1 = match.group(1).replaceAll(" ", "");
- String matI = mat1.replaceAll("i", "");
- if (match.group(1).matches("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i")) {
- imag = Double.parseDouble(matI);
- real = 0;
- } else if (match.group(6) != null) {
- String mat6 = match.group(6).replaceAll(" ", "");
- real = Double.parseDouble(mat1);
- imag = Double.parseDouble(mat6);
- } else {
- real = Double.parseDouble(mat1);
- imag = 0;
- }
- }
- while (match1.find()) {
- String mat1 = match1.group(1).replaceAll(" ", "");
- String matI = mat1.replaceAll("i", "");
- if (match1.group(1).matches("(-? *(\\d+(\\.\\d*)|\\d+|\\.\\d+)) *i")) {
- imag2 = Double.parseDouble(matI);
- real2 = 0;
- } else if (match1.group(6) != null) {
- String mat6 = match1.group(6).replaceAll(" ", "");
- real2 = Double.parseDouble(mat1);
- imag2 = Double.parseDouble(mat6);
- } else {
- real2 = Double.parseDouble(mat1);
- imag2 = 0;
- }
- }
- Complex c1 = new Complex(real, imag);
- Complex c2 = new Complex(real2, imag2);
- String options[] = { "Add", "Subtract", "Multiply", "Divide", "Negate", "Conjugate", "Absolute Value",
- "Distance", "Equals", "A Greater Than B", "A Less Than B", "Quit" };
- int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.PLAIN_MESSAGE, null, options, 11);
- output = "";
- switch (option) {
- case 0:
- Complex plus = c1.add(c2);
- output += (c1.toString(c1) + " + " + c2.toString(c2) + " = " + plus.toString(plus));
- break;
- case 1:
- Complex minus = c1.subtract(c2);
- output += (c1.toString(c1) + " - " + c2.toString(c2) + " = " + minus.toString(minus));
- break;
- case 2:
- Complex times = c1.multiply(c2);
- output += (c1.toString(c1) + " * " + c2.toString(c2) + " = " + times.toString(times));
- break;
- case 3:
- Complex quot = c1.divide(c2);
- output += (c1.toString(c1) + " / " + c2.toString(c2) + " = " + quot.toString(quot));
- break;
- case 4:
- output += ("-(" + c1.toString(c1) + " ) = " + (c1.negate()).toString(c1.negate()) + "\n");
- output += ("-(" + c2.toString(c2) + " ) = " + (c2.negate()).toString(c2.negate()));
- break;
- case 5:
- output += (c1.toString(c1) + " conjugated = " + (c1.conjugate()).toString(c1.conjugate()) + "\n");
- output += (c2.toString(c2) + " conjugated = " + (c2.conjugate()).toString(c2.conjugate()));
- break;
- case 6:
- output += ("|" + c1.toString(c1) + "| =" + c1.abs() + "\n");
- output += ("|" + c2.toString(c2) + "| =" + c2.abs());
- break;
- case 7:
- output += ("Distance of (" + c1.toString(c1) + ") to (" + c2.toString(c2) + ") = " + c1.distance(c2));
- break;
- case 8:
- output += ("Does " + c1.toString(c1) + " = " + c2.toString(c2) + "?\n" + c1.equals(c2));
- break;
- case 9:
- output += ("Is " + c1.toString(c1) + " Greater than " + c2.toString(c2) + " ?\n" + c1.greaterThan(c2));
- break;
- case 10:
- output += ("Is " + c1.toString(c1) + " Less than " + c2.toString(c2) + " ?\n" + c1.lessThan(c2));
- break;
- }
- if (option == 11)
- break;
- JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.PLAIN_MESSAGE);
- 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;
- }
- }
- }
Add Comment
Please, Sign In to add comment