Advertisement
Omar_Natour

Natour, O. 2/22/16 Csc-112 Web complex numbers

Feb 22nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ComplexNumberDriver {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         final String TITLE = "Complex Numbers";
  8.  
  9.         double real;
  10.         double imag;
  11.  
  12.         String output;
  13.  
  14.         while (true) {
  15.  
  16.             String tempR = null;
  17.             String tempI = null;
  18.  
  19.             tempR = JOptionPane.showInputDialog(null, "Enter a value for the first part of complex number a.", TITLE, JOptionPane.INFORMATION_MESSAGE);
  20.             if (tempR == null)
  21.                 break;
  22.             real = Double.parseDouble(tempR);
  23.  
  24.             tempI = JOptionPane.showInputDialog(null, "Enter a value for the imaginary part of complex number a.", TITLE, JOptionPane.PLAIN_MESSAGE);
  25.             if (tempI == null)
  26.                 break;
  27.             imag = Double.parseDouble(tempI);
  28.             Complex c1 = new Complex(real, imag);
  29.  
  30.             tempR = JOptionPane.showInputDialog(null, "Enter a value for the first part of complex number b.", TITLE, JOptionPane.PLAIN_MESSAGE);
  31.             if (tempR == null)
  32.                 break;
  33.             real = Double.parseDouble(tempR);
  34.  
  35.             tempI = JOptionPane.showInputDialog(null, "Enter a value for the imaginary part of complex number b.",TITLE, JOptionPane.PLAIN_MESSAGE);
  36.             if (tempI == null)
  37.                 break;
  38.             imag = Double.parseDouble(tempI);
  39.             Complex c2 = new Complex(real, imag);
  40.  
  41.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Negate", "Conjugate", "Absolute Value",
  42.                     "Distance", "Equals", "A Greater Than B", "A Less Than B", "Quit" };
  43.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  44.                     JOptionPane.PLAIN_MESSAGE, null, options, 11);
  45.  
  46.             output = "";
  47.             switch (option) {
  48.             case 0:
  49.                 Complex plus = c1.add(c2);
  50.                 output += (c1.toString(c1) + " + " + c2.toString(c2) + " = " + plus.toString(plus));
  51.                 break;
  52.             case 1:
  53.                 Complex minus = c1.subtract(c2);
  54.                 output += (c1.toString(c1) + " - " + c2.toString(c2) + " = " + minus.toString(minus));
  55.                 break;
  56.             case 2:
  57.                 Complex times = c1.multiply(c2);
  58.                 output += (c1.toString(c1) + " * " + c2.toString(c2) + " = " + times.toString(times));
  59.                 break;
  60.             case 3:
  61.                 Complex quot = c1.divide(c2);
  62.                 output += (c1.toString(c1) + " / " + c2.toString(c2) + " = " + quot.toString(quot));
  63.                 break;
  64.             case 4:
  65.                 output += ("-(" + c1.toString(c1) + " ) = " + (c1.negate()).toString(c1.negate()) + "\n");
  66.                 output += ("-(" + c2.toString(c2) + " ) = " + (c2.negate()).toString(c2.negate()));
  67.                 break;
  68.             case 5:
  69.                 output += (c1.toString(c1) + " conjugated = " + (c1.conjugate()).toString(c1.conjugate()) + "\n");
  70.                 output += (c2.toString(c2) + " conjugated = " + (c2.conjugate()).toString(c2.conjugate()));
  71.                 break;
  72.             case 6:
  73.                 output += ("|" + c1.toString(c1) + "| =" + c1.abs() + "\n");
  74.                 output += ("|" + c2.toString(c2) + "| =" + c2.abs());
  75.                 break;
  76.             case 7:
  77.                 output += ("Distance of (" + c1.toString(c1) + ") to (" + c2.toString(c2) + ") = " + c1.distance(c2));
  78.                 break;
  79.             case 8:
  80.                 output += ("Does " + c1.toString(c1) + " = " + c2.toString(c2) + "?\n" + c1.equals(c2));
  81.                 break;
  82.             case 9:
  83.                 output += ("Is " + c1.toString(c1) + " Greater than " + c2.toString(c2) + " ?\n" + c1.greaterThan(c2));
  84.                 break;
  85.             case 10:
  86.                 output += ("Is " + c1.toString(c1) + " Less than " + c2.toString(c2) + " ?\n" + c1.lessThan(c2));
  87.                 break;
  88.             }
  89.             if (option == 11)
  90.                 break;
  91.             JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.PLAIN_MESSAGE);
  92.  
  93.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
  94.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  95.             if (option != JOptionPane.YES_OPTION)
  96.                 break;
  97.  
  98.         }
  99.  
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement