Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *
- * Lu Liu
- * CSC-112 Intermediate Java Programming
- * FEB 18 2016
- * lliu0001@ student.stcc.edu
- *
- * Complex Numbers Gui Driver:
- * 1)test client;
- * 2)use method to generate two Complex Number a and b, and test them;
- * 3)operate Complex Number a and b;
- *
- * 4)method: generate Complex Number;
- * 5)method: test the methods: abs(), negate(), conjugate();
- */
- package Complex;
- import javax.swing.JOptionPane;
- public class ComplexNumberGuiDriver {
- // test client
- public static void main(String[] args) {
- final String TITLE = "ComplexNumber Demo";
- JOptionPane.showMessageDialog(null, "Running " + TITLE + "\nHave Fun!!!", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- do {
- // generate two Complex Number a and b, and test them;
- JOptionPane.showMessageDialog(null, "Generate Complex Number 1: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
- Complex a = generateComplexNumber();
- test(a);
- JOptionPane.showMessageDialog(null, "Generate Complex Number 2: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
- Complex b = generateComplexNumber();
- test(b);
- // operate Complex Number a and b;
- Complex ans = null;
- String options[] = { "Add", "Subtract", "Multiply", "Divide", "Distance", "Compare", "Quit" };
- int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.PLAIN_MESSAGE, null, options, 4);
- String output = "";
- switch (option) {
- case 0:
- ans = a.add(b);
- output = " + ";
- break;
- case 1:
- ans = a.subtract(b);
- output = " - ";
- break;
- case 2:
- ans = a.multiply(b);
- output = " * ";
- break;
- case 3:
- ans = a.divide(b);
- output = " / ";
- break;
- default:
- break;
- }
- if (option == 4) {
- JOptionPane.showMessageDialog(null, a + " and " + b + " 's diatance = " + a.distance(b), TITLE,
- JOptionPane.PLAIN_MESSAGE);
- } else if (option == 5) {
- if (a.equals(b))
- JOptionPane.showMessageDialog(null, a + " is equals to " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- else if (a.greaterThan(b))
- JOptionPane.showMessageDialog(null, a + " is greater than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- else
- JOptionPane.showMessageDialog(null, a + " is less than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
- } else if (option > 4)
- break;
- else {
- String sen = "(" + a + ")" + output + "(" + b + ") " + " = " + ans;
- JOptionPane.showMessageDialog(null, sen, TITLE, JOptionPane.PLAIN_MESSAGE);
- }
- // do this again?
- 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;
- } while (true);
- }
- // generate Complex Number
- public static Complex generateComplexNumber() {
- double real, imag;
- final String TITLE = "ComplexNumber Demo";
- String temp;
- temp = JOptionPane.showInputDialog(null, "Enter Real of Complex Number", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- if (temp == null)
- return null;
- real = Double.parseDouble(temp);
- temp = JOptionPane.showInputDialog(null, "Enter Imaginary Components of Complex Number", TITLE,
- JOptionPane.INFORMATION_MESSAGE);
- if (temp == null)
- return null;
- imag = Double.parseDouble(temp);
- Complex a = new Complex(real, imag);
- return a;
- }
- // test the methods: abs(), negate(), conjugate()
- public static void test(Complex a) {
- final String TITLE = "Test Complex Number";
- do {
- Complex ans1 = null;
- String options1[] = { "Negate", "Conjugate", "Abs", "Quit" };
- int option = JOptionPane.showOptionDialog(null, a.toString(), TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.PLAIN_MESSAGE, null, options1, 3);
- switch (option) {
- case 0:
- ans1 = a.negate();
- JOptionPane.showMessageDialog(null, a + "'s negation is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
- break;
- case 1:
- ans1 = a.conjugate();
- JOptionPane.showMessageDialog(null, a + "'s conjugate is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
- break;
- case 2:
- double dis = a.abs();
- JOptionPane.showMessageDialog(null, a + "'s distance is " + dis, TITLE, JOptionPane.PLAIN_MESSAGE);
- break;
- default:
- break;
- }
- if (option > 3)
- break;
- option = JOptionPane.showConfirmDialog(null, "Do you want test again?", TITLE, JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (option != JOptionPane.YES_OPTION)
- break;
- } while (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement