Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chapter09;
- 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);
- }
- }
- class Complex {
- // define two doubles, known as the real part and the imaginary part.
- private double real;
- private double imag;
- // define 4 constructors
- public Complex(double real, double imag) {
- this.real = real;
- this.imag = imag;
- }
- public Complex(double real) {
- this.real = real;
- this.imag = 0;
- }
- public Complex() {
- this(0, 0);
- }
- // the constructor takes a String
- public Complex(String str) {
- String newStr = str.trim();
- char[] chars = newStr.toCharArray();
- if (newStr.indexOf("i") == -1)
- this.real = Double.parseDouble(newStr);
- else if (chars.length == 1) {
- this.imag = 1.0;
- } else if (chars.length == 2) {
- this.imag = (double) (chars[0] - '0');
- } else if (chars.length == 3) {
- if (Character.isDigit(chars[0])) {
- this.real = (double) (chars[0] - '0');
- this.imag = (chars[1] == '-') ? -1.0 : 1.0;
- } else
- this.imag = -(double) (chars[1] - '0');
- } else if (chars.length == 4) {
- if (Character.isDigit(chars[0])) {
- this.real = (double) (chars[0] - '0');
- this.imag = (chars[1] == '-') ? -(double) (chars[2] - '0') : (double) (chars[2] - '0');
- } else {
- this.real = -(double) (chars[1] - '0');
- this.imag = (chars[2] == '-') ? -1.0 : 1.0;
- }
- } else if (chars.length == 5) {
- this.real = -(double) (chars[1] - '0');
- this.imag = (chars[2] == '-') ? -(double) (chars[3] - '0') : (double)(chars[3] - '0');
- }
- }
- // define public methods
- public double getReal() {
- return real;
- }
- public double getImag() {
- return imag;
- }
- public Complex add(Complex c) {
- return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
- }
- public Complex subtract(Complex c) {
- return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
- }
- public Complex multiply(Complex c) {
- return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
- (this.real * c.getImag() + c.getImag() * c.getImag()));
- }
- public Complex divide(Complex c) {
- double a = this.real;
- double b = this.imag;
- double x = c.getReal();
- double y = c.getImag();
- return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
- }
- public double abs() {
- double a = this.getReal();
- double b = this.getImag();
- return Math.sqrt(a * a + b * b);
- }
- public Complex negate() {
- double a = this.getReal();
- double b = this.getImag();
- return new Complex(-a, -b);
- }
- public Complex conjugate() {
- return new Complex(this.getReal(), -this.getImag());
- }
- public double distance(Complex c) {
- double x = this.getReal() - c.getReal();
- double y = this.getImag() - c.getImag();
- return x * x + y * y;
- }
- public boolean equals(Complex c) {
- double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
- if (minus / this.abs() < 1E-6)
- return true;
- return false;
- }
- public boolean greaterThan(Complex c) {
- return (this.abs() > c.abs()) ? true : false;
- }
- public boolean lessThan(Complex c) {
- return !this.greaterThan(c);
- }
- public String toString() {
- if (this.getImag() > 0)
- return this.getReal() + "+" + (this.getImag() + "i");
- else if (this.getImag() == 0)
- return this.getReal() + "";
- else
- return this.getReal() + "" + (this.getImag() + "i");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement