Advertisement
rjsantiago0001

ComplexGUIDriver for String Constructor

Feb 29th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. /* Ricardo Santiago
  2. * 2/20/16
  3. * CSC-112
  4. * ComplexGuiDriver
  5. */
  6. import javax.swing.JOptionPane;
  7.  
  8. public class ComplexGuiDriver {
  9.  
  10.     public static void main(String[] args) {
  11.         final String title = "Complex Numbers GUI";
  12.         System.out.println("Running " + title + ". Have fun!!!");
  13.  
  14.         do {
  15.  
  16.             String temp;
  17.             temp = JOptionPane.showInputDialog(null, "Enter 1st complex number", title,
  18.                     JOptionPane.INFORMATION_MESSAGE);
  19.             Complex a = new Complex(temp);
  20.             if (temp == null)
  21.                 break;
  22.  
  23.             temp = JOptionPane.showInputDialog(null, "Enter 2nd complex number", title,
  24.                     JOptionPane.INFORMATION_MESSAGE);
  25.             Complex c = new Complex(temp);
  26.             if (temp == null)
  27.                 break;
  28.  
  29.             Complex ans = null;
  30.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Absolute Value", "Negate", "Conjugate",
  31.                     "Distance", "Equality", "Greater than", "Less Than", "Quit" };
  32.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", title, JOptionPane.YES_NO_CANCEL_OPTION,
  33.                     JOptionPane.PLAIN_MESSAGE, null, options, 11);
  34.             String output = "(" + a + ")";
  35.             String out = "";
  36.             switch (option) {
  37.             case 0:
  38.                 ans = a.add(c);
  39.                 output += " + ";
  40.                 break;
  41.             case 1:
  42.                 ans = a.subtract(c);
  43.                 output += " + ";
  44.                 break;
  45.             case 2:
  46.                 ans = a.multiply(c);
  47.                 output += " * ";
  48.                 break;
  49.             case 3:
  50.                 ans = a.divide(c);
  51.                 output += " / ";
  52.                 break;
  53.             case 4:
  54.                 out += " |" + a + "|  = " + a.abs() + " \n |" + c + "| = " + c.abs() + "";
  55.                 break;
  56.             case 5:
  57.                 out += "negate " + a + " = " + a.negate() + "\nnegate " + c + " = " + c.negate();
  58.                 break;
  59.             case 6:
  60.                 out += "conjugate of " + a + " = " + a.conjugate() + "\nconjugate of " + c + " = " + c.conjugate();
  61.                 break;
  62.             case 7:
  63.                 out += "distance between (" + a + ") and (" + c + ") = " + a.distance(c);
  64.                 break;
  65.             case 8:
  66.                 out += a + " == " + c + " = " + a.equals(c);
  67.                 break;
  68.             case 9:
  69.                 out += a + " > " + c + " = " + a.greaterThan(c);
  70.                 break;
  71.             case 10:
  72.                 out += a + " < " + c + " = " + a.lessThan(c);
  73.                 break;
  74.  
  75.             default:
  76.                 break;
  77.             }
  78.             if (option > 11)
  79.                 break;
  80.  
  81.             if (option <= 3) {
  82.                 output += "(" + c + ")" + " = " + ans;
  83.                 JOptionPane.showMessageDialog(null, output, title, JOptionPane.PLAIN_MESSAGE);
  84.  
  85.             } else {
  86.                 if (option >= 4)
  87.                     JOptionPane.showMessageDialog(null, out, title, JOptionPane.PLAIN_MESSAGE);
  88.             }
  89.  
  90.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", title,
  91.                     JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
  92.             if (option != JOptionPane.YES_OPTION)
  93.                 break;
  94.         } while (true);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement