Advertisement
luliu

Complex Number Gui Driver

Feb 18th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. *
  2.  * Lu Liu
  3.  * CSC-112 Intermediate Java Programming
  4.  * FEB 18 2016
  5.  * lliu0001@ student.stcc.edu
  6.  *
  7.  * Complex Numbers Gui Driver:
  8.  * 1)test client;
  9.  * 2)use method to generate two Complex Number a and b, and test them;
  10.  * 3)operate Complex Number a and b;
  11.  *
  12.  * 4)method: generate Complex Number;
  13.  * 5)method: test the methods: abs(), negate(), conjugate();
  14.  */
  15. package Complex;
  16.  
  17. import javax.swing.JOptionPane;
  18.  
  19. public class ComplexNumberGuiDriver {
  20.  
  21.     // test client
  22.     public static void main(String[] args) {
  23.         final String TITLE = "ComplexNumber Demo";
  24.         JOptionPane.showMessageDialog(null, "Running " + TITLE + "\nHave Fun!!!", TITLE,
  25.                 JOptionPane.INFORMATION_MESSAGE);
  26.  
  27.         do {
  28.             // generate two Complex Number a and b, and test them;
  29.             JOptionPane.showMessageDialog(null, "Generate Complex Number 1: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
  30.             Complex a = generateComplexNumber();
  31.             test(a);
  32.  
  33.             JOptionPane.showMessageDialog(null, "Generate Complex Number 2: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
  34.             Complex b = generateComplexNumber();
  35.             test(b);
  36.  
  37.             // operate Complex Number a and b;
  38.             Complex ans = null;
  39.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Distance", "Compare", "Quit" };
  40.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  41.                     JOptionPane.PLAIN_MESSAGE, null, options, 4);
  42.             String output = "";
  43.             switch (option) {
  44.             case 0:
  45.                 ans = a.add(b);
  46.                 output = " + ";
  47.                 break;
  48.             case 1:
  49.                 ans = a.subtract(b);
  50.                 output = " - ";
  51.                 break;
  52.             case 2:
  53.                 ans = a.multiply(b);
  54.                 output = " * ";
  55.                 break;
  56.             case 3:
  57.                 ans = a.divide(b);
  58.                 output = " / ";
  59.                 break;
  60.             default:
  61.                 break;
  62.             }
  63.             if (option == 4) {
  64.                 JOptionPane.showMessageDialog(null, a + " and " + b + " 's diatance = " + a.distance(b), TITLE,
  65.                         JOptionPane.PLAIN_MESSAGE);
  66.             } else if (option == 5) {
  67.                 if (a.equals(b))
  68.                     JOptionPane.showMessageDialog(null, a + " is equals to " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  69.                 else if (a.greaterThan(b))
  70.                     JOptionPane.showMessageDialog(null, a + " is greater than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  71.                 else
  72.                     JOptionPane.showMessageDialog(null, a + " is less than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  73.  
  74.             } else if (option > 4)
  75.                 break;
  76.             else {
  77.                 String sen = "(" + a + ")" + output + "(" + b + ") " + " = " + ans;
  78.                 JOptionPane.showMessageDialog(null, sen, TITLE, JOptionPane.PLAIN_MESSAGE);
  79.             }
  80.  
  81.             // do this again?
  82.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
  83.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  84.             if (option != JOptionPane.YES_OPTION)
  85.                 break;
  86.         } while (true);
  87.     }
  88.  
  89.     // generate Complex Number
  90.     public static Complex generateComplexNumber() {
  91.         double real, imag;
  92.         final String TITLE = "ComplexNumber Demo";
  93.         String temp;
  94.         temp = JOptionPane.showInputDialog(null, "Enter Real of Complex Number", TITLE,
  95.                 JOptionPane.INFORMATION_MESSAGE);
  96.         if (temp == null)
  97.             return null;
  98.         real = Double.parseDouble(temp);
  99.  
  100.         temp = JOptionPane.showInputDialog(null, "Enter Imaginary Components of Complex Number", TITLE,
  101.                 JOptionPane.INFORMATION_MESSAGE);
  102.         if (temp == null)
  103.             return null;
  104.         imag = Double.parseDouble(temp);
  105.         Complex a = new Complex(real, imag);
  106.         return a;
  107.     }
  108.  
  109.     // test the methods: abs(), negate(), conjugate()
  110.     public static void test(Complex a) {
  111.         final String TITLE = "Test Complex Number";
  112.  
  113.         do {
  114.             Complex ans1 = null;
  115.             String options1[] = { "Negate", "Conjugate", "Abs", "Quit" };
  116.             int option = JOptionPane.showOptionDialog(null, a.toString(), TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  117.                     JOptionPane.PLAIN_MESSAGE, null, options1, 3);
  118.             switch (option) {
  119.             case 0:
  120.                 ans1 = a.negate();
  121.                 JOptionPane.showMessageDialog(null, a + "'s negation is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
  122.                 break;
  123.             case 1:
  124.                 ans1 = a.conjugate();
  125.                 JOptionPane.showMessageDialog(null, a + "'s conjugate is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
  126.                 break;
  127.             case 2:
  128.                 double dis = a.abs();
  129.                 JOptionPane.showMessageDialog(null, a + "'s distance is " + dis, TITLE, JOptionPane.PLAIN_MESSAGE);
  130.                 break;
  131.             default:
  132.                 break;
  133.             }
  134.             if (option > 3)
  135.                 break;
  136.             option = JOptionPane.showConfirmDialog(null, "Do you want test again?", TITLE, JOptionPane.YES_NO_OPTION,
  137.                     JOptionPane.QUESTION_MESSAGE);
  138.             if (option != JOptionPane.YES_OPTION)
  139.                 break;
  140.         } while (true);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement