Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class ComplexGUI {
- private final static String TITLE = "Complex Number Program";
- private final static String CONTINUE_PROMPT = "Do this again?";
- // **********************************************
- private static void testComplex(Complex c1, Complex c2) {
- Complex plus = c1.add(c2);
- Complex minus = c1.subtract(c2);
- Complex times = c1.multiply(c2);
- Complex quot = c1.divide(c2);
- String output = "";
- output += String.format("c1 = " + c1 + "\n");
- output += String.format("c2 = " + c2 + "\n");
- output += String.format("c1 + c2 = " + plus + "\n");
- output += String.format("c1 - c2 = " + minus + "\n");
- output += String.format("c1 * c2 = " + times + "\n");
- output += String.format("c1 / c2 = " + quot + "\n");
- output += String.format("-c1 = " + c1.negate() + "\n");
- output += String.format("-c2 = " + c2.negate() + "\n");
- output += String.format("Conj c1 = " + c1.conjugate() + "\n");
- output += String.format("Conj c2 = " + c2.conjugate() + "\n");
- output += String.format("|c1| = " + c1.abs() + "\n");
- output += String.format("|c2| = " + c2.abs() + "\n");
- output += String.format("Dist = " + c1.distance(c2) + "\n");
- output += String.format("==? = " + c1.equals(c2) + "\n");
- output += String.format(">? = " + c1.greaterThan(c2) + "\n");
- output += String.format("<? = " + c1.lessThan(c2) + "\n");
- JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.INFORMATION_MESSAGE, null);
- }
- // **********************************************
- private static Complex buildComplex(String prompt) {
- Complex comp = null;
- ComplexDriverPanel cip = new ComplexDriverPanel();
- int result = JOptionPane.showConfirmDialog(null, cip, "Please Enter Values For " + prompt,
- JOptionPane.OK_CANCEL_OPTION);
- if (result == JOptionPane.OK_OPTION) {
- double real = cip.getReal();
- double imag = cip.getImag();
- comp = new Complex(real, imag);
- } else {
- System.exit(1);
- }
- return comp;
- }
- // **********************************************
- private static void process(String args[]) {
- Complex c1 = buildComplex("Complex Number 1");
- Complex c2 = buildComplex("Complex Number 2");
- testComplex(c1, c2);
- }
- // **********************************************
- private static boolean doThisAgain(String prompt) {
- int option = JOptionPane.showConfirmDialog(null, prompt, TITLE, JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- return option == JOptionPane.YES_OPTION;
- }
- // **********************************************
- public static void main(String args[]) {
- do {
- process(args);
- } while (doThisAgain(CONTINUE_PROMPT));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement