Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class ComplexNumberDriver {
- public static void main(String[] args) {
- final String TITLE = "Complex Numbers";
- double real;
- double imag;
- String output;
- while (true) {
- String tempR = null;
- String tempI = null;
- tempR = JOptionPane.showInputDialog(null, "Enter a value for the first part of complex number a.", TITLE, JOptionPane.INFORMATION_MESSAGE);
- if (tempR == null)
- break;
- real = Double.parseDouble(tempR);
- tempI = JOptionPane.showInputDialog(null, "Enter a value for the imaginary part of complex number a.", TITLE, JOptionPane.PLAIN_MESSAGE);
- if (tempI == null)
- break;
- imag = Double.parseDouble(tempI);
- Complex c1 = new Complex(real, imag);
- tempR = JOptionPane.showInputDialog(null, "Enter a value for the first part of complex number b.", TITLE, JOptionPane.PLAIN_MESSAGE);
- if (tempR == null)
- break;
- real = Double.parseDouble(tempR);
- tempI = JOptionPane.showInputDialog(null, "Enter a value for the imaginary part of complex number b.",TITLE, JOptionPane.PLAIN_MESSAGE);
- if (tempI == null)
- break;
- imag = Double.parseDouble(tempI);
- Complex c2 = new Complex(real, imag);
- String options[] = { "Add", "Subtract", "Multiply", "Divide", "Negate", "Conjugate", "Absolute Value",
- "Distance", "Equals", "A Greater Than B", "A Less Than B", "Quit" };
- int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.PLAIN_MESSAGE, null, options, 11);
- output = "";
- switch (option) {
- case 0:
- Complex plus = c1.add(c2);
- output += (c1.toString(c1) + " + " + c2.toString(c2) + " = " + plus.toString(plus));
- break;
- case 1:
- Complex minus = c1.subtract(c2);
- output += (c1.toString(c1) + " - " + c2.toString(c2) + " = " + minus.toString(minus));
- break;
- case 2:
- Complex times = c1.multiply(c2);
- output += (c1.toString(c1) + " * " + c2.toString(c2) + " = " + times.toString(times));
- break;
- case 3:
- Complex quot = c1.divide(c2);
- output += (c1.toString(c1) + " / " + c2.toString(c2) + " = " + quot.toString(quot));
- break;
- case 4:
- output += ("-(" + c1.toString(c1) + " ) = " + (c1.negate()).toString(c1.negate()) + "\n");
- output += ("-(" + c2.toString(c2) + " ) = " + (c2.negate()).toString(c2.negate()));
- break;
- case 5:
- output += (c1.toString(c1) + " conjugated = " + (c1.conjugate()).toString(c1.conjugate()) + "\n");
- output += (c2.toString(c2) + " conjugated = " + (c2.conjugate()).toString(c2.conjugate()));
- break;
- case 6:
- output += ("|" + c1.toString(c1) + "| =" + c1.abs() + "\n");
- output += ("|" + c2.toString(c2) + "| =" + c2.abs());
- break;
- case 7:
- output += ("Distance of (" + c1.toString(c1) + ") to (" + c2.toString(c2) + ") = " + c1.distance(c2));
- break;
- case 8:
- output += ("Does " + c1.toString(c1) + " = " + c2.toString(c2) + "?\n" + c1.equals(c2));
- break;
- case 9:
- output += ("Is " + c1.toString(c1) + " Greater than " + c2.toString(c2) + " ?\n" + c1.greaterThan(c2));
- break;
- case 10:
- output += ("Is " + c1.toString(c1) + " Less than " + c2.toString(c2) + " ?\n" + c1.lessThan(c2));
- break;
- }
- if (option == 11)
- break;
- JOptionPane.showMessageDialog(null, output, TITLE, JOptionPane.PLAIN_MESSAGE);
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement