Advertisement
Guest User

Complex Number GUI

a guest
Feb 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ComplexGUI {
  4.  
  5.     private final static String TITLE = "Complex Number Program";
  6.     private final static String CONTINUE_PROMPT = "Do this again?";
  7.  
  8.     // **********************************************
  9.  
  10.     private static void testComplex(Complex c1, Complex c2) {
  11.         Complex plus = c1.add(c2);
  12.         Complex minus = c1.subtract(c2);
  13.         Complex times = c1.multiply(c2);
  14.         Complex quot = c1.divide(c2);
  15.        
  16.         String output = "";
  17.         output += String.format("c1      = " + c1 + "\n");
  18.         output += String.format("c2      = " + c2 + "\n");
  19.         output += String.format("c1 + c2 = " + plus + "\n");
  20.         output += String.format("c1 - c2 = " + minus + "\n");
  21.         output += String.format("c1 * c2 = " + times + "\n");
  22.         output += String.format("c1 / c2 = " + quot + "\n");
  23.         output += String.format("-c1     = " + c1.negate() + "\n");
  24.         output += String.format("-c2     = " + c2.negate() + "\n");
  25.         output += String.format("Conj c1 = " + c1.conjugate() + "\n");
  26.         output += String.format("Conj c2 = " + c2.conjugate() + "\n");
  27.         output += String.format("|c1|    = " + c1.abs() + "\n");
  28.         output += String.format("|c2|    = " + c2.abs() + "\n");
  29.         output += String.format("Dist    = " + c1.distance(c2) + "\n");
  30.         output += String.format("==?     = " + c1.equals(c2) + "\n");
  31.         output += String.format(">?      = " + c1.greaterThan(c2) + "\n");
  32.         output += String.format("<?      = " + c1.lessThan(c2) + "\n");
  33.        
  34.         JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.INFORMATION_MESSAGE, null);
  35.     }
  36.  
  37.  
  38.     // **********************************************
  39.  
  40.     private static Complex buildComplex(String prompt) {
  41.         Complex comp = null;
  42.        
  43.         ComplexDriverPanel cip = new ComplexDriverPanel();
  44.         int result = JOptionPane.showConfirmDialog(null, cip, "Please Enter Values For " + prompt,
  45.                 JOptionPane.OK_CANCEL_OPTION);
  46.         if (result == JOptionPane.OK_OPTION) {
  47.             double real = cip.getReal();
  48.             double imag = cip.getImag();
  49.             comp = new Complex(real, imag);
  50.         } else {
  51.             System.exit(1);
  52.         }
  53.         return comp;
  54.     }
  55.  
  56.     // **********************************************
  57.  
  58.     private static void process(String args[]) {
  59.         Complex c1 = buildComplex("Complex Number 1");
  60.         Complex c2 = buildComplex("Complex Number 2");
  61.         testComplex(c1, c2);
  62.     }
  63.        
  64.  
  65.     // **********************************************
  66.  
  67.     private static boolean doThisAgain(String prompt) {
  68.         int option = JOptionPane.showConfirmDialog(null, prompt, TITLE, JOptionPane.YES_NO_OPTION,
  69.                 JOptionPane.QUESTION_MESSAGE);
  70.         return option == JOptionPane.YES_OPTION;
  71.     }
  72.  
  73.     // **********************************************
  74.  
  75.     public static void main(String args[]) {
  76.         do {
  77.             process(args);
  78.         } while (doThisAgain(CONTINUE_PROMPT));
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement