Advertisement
luliu

Complex Numbers

Feb 18th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.45 KB | None | 0 0
  1. package chapter09;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class ComplexNumberGuiDriver {
  6.  
  7.     // test client
  8.     public static void main(String[] args) {
  9.         final String TITLE = "ComplexNumber Demo";
  10.         JOptionPane.showMessageDialog(null, "Running " + TITLE + "\nHave Fun!!!", TITLE,
  11.                 JOptionPane.INFORMATION_MESSAGE);
  12.  
  13.         do {
  14.             // generate two Complex Number a and b, and test them;
  15.             JOptionPane.showMessageDialog(null, "Generate Complex Number 1: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
  16.             Complex a = generateComplexNumber();
  17.             test(a);
  18.  
  19.             JOptionPane.showMessageDialog(null, "Generate Complex Number 2: ", TITLE, JOptionPane.INFORMATION_MESSAGE);
  20.             Complex b = generateComplexNumber();
  21.             test(b);
  22.  
  23.             // operate Complex Number a and b;
  24.             Complex ans = null;
  25.             String options[] = { "Add", "Subtract", "Multiply", "Divide", "Distance", "Compare", "Quit" };
  26.             int option = JOptionPane.showOptionDialog(null, "Choose Operation", TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  27.                     JOptionPane.PLAIN_MESSAGE, null, options, 4);
  28.             String output = "";
  29.             switch (option) {
  30.             case 0:
  31.                 ans = a.add(b);
  32.                 output = " + ";
  33.                 break;
  34.             case 1:
  35.                 ans = a.subtract(b);
  36.                 output = " - ";
  37.                 break;
  38.             case 2:
  39.                 ans = a.multiply(b);
  40.                 output = " * ";
  41.                 break;
  42.             case 3:
  43.                 ans = a.divide(b);
  44.                 output = " / ";
  45.                 break;
  46.             default:
  47.                 break;
  48.             }
  49.             if (option == 4) {
  50.                 JOptionPane.showMessageDialog(null, a + " and " + b + " 's diatance = " + a.distance(b), TITLE,
  51.                         JOptionPane.PLAIN_MESSAGE);
  52.             } else if (option == 5) {
  53.                 if (a.equals(b))
  54.                     JOptionPane.showMessageDialog(null, a + " is equals to " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  55.                 else if (a.greaterThan(b))
  56.                     JOptionPane.showMessageDialog(null, a + " is greater than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  57.                 else
  58.                     JOptionPane.showMessageDialog(null, a + " is less than " + b, TITLE, JOptionPane.PLAIN_MESSAGE);
  59.  
  60.             } else if (option > 4)
  61.                 break;
  62.             else {
  63.                 String sen = "(" + a + ")" + output + "(" + b + ") " + " = " + ans;
  64.                 JOptionPane.showMessageDialog(null, sen, TITLE, JOptionPane.PLAIN_MESSAGE);
  65.             }
  66.  
  67.             // do this again?
  68.             option = JOptionPane.showConfirmDialog(null, "Do you want to do this again?", TITLE,
  69.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  70.             if (option != JOptionPane.YES_OPTION)
  71.                 break;
  72.         } while (true);
  73.     }
  74.  
  75.     // generate Complex Number
  76.     public static Complex generateComplexNumber() {
  77.         double real, imag;
  78.         final String TITLE = "ComplexNumber Demo";
  79.         String temp;
  80.         temp = JOptionPane.showInputDialog(null, "Enter Real of Complex Number", TITLE,
  81.                 JOptionPane.INFORMATION_MESSAGE);
  82.         if (temp == null)
  83.             return null;
  84.         real = Double.parseDouble(temp);
  85.  
  86.         temp = JOptionPane.showInputDialog(null, "Enter Imaginary Components of Complex Number", TITLE,
  87.                 JOptionPane.INFORMATION_MESSAGE);
  88.         if (temp == null)
  89.             return null;
  90.         imag = Double.parseDouble(temp);
  91.         Complex a = new Complex(real, imag);
  92.         return a;
  93.     }
  94.  
  95.     // test the methods: abs(), negate(), conjugate()
  96.     public static void test(Complex a) {
  97.         final String TITLE = "Test Complex Number";
  98.  
  99.         do {
  100.             Complex ans1 = null;
  101.             String options1[] = { "Negate", "Conjugate", "Abs", "Quit" };
  102.             int option = JOptionPane.showOptionDialog(null, a.toString(), TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
  103.                     JOptionPane.PLAIN_MESSAGE, null, options1, 3);
  104.             switch (option) {
  105.             case 0:
  106.                 ans1 = a.negate();
  107.                 JOptionPane.showMessageDialog(null, a + "'s negation is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
  108.                 break;
  109.             case 1:
  110.                 ans1 = a.conjugate();
  111.                 JOptionPane.showMessageDialog(null, a + "'s conjugate is " + ans1, TITLE, JOptionPane.PLAIN_MESSAGE);
  112.                 break;
  113.             case 2:
  114.                 double dis = a.abs();
  115.                 JOptionPane.showMessageDialog(null, a + "'s distance is " + dis, TITLE, JOptionPane.PLAIN_MESSAGE);
  116.                 break;
  117.             default:
  118.                 break;
  119.             }
  120.             if (option > 3)
  121.                 break;
  122.             option = JOptionPane.showConfirmDialog(null, "Do you want test again?", TITLE, JOptionPane.YES_NO_OPTION,
  123.                     JOptionPane.QUESTION_MESSAGE);
  124.             if (option != JOptionPane.YES_OPTION)
  125.                 break;
  126.         } while (true);
  127.     }
  128. }
  129.  
  130. class Complex {
  131.  
  132.     // define two doubles, known as the real part and the imaginary part.
  133.     private double real;
  134.     private double imag;
  135.  
  136.     // define 4 constructors
  137.     public Complex(double real, double imag) {
  138.         this.real = real;
  139.         this.imag = imag;
  140.     }
  141.  
  142.     public Complex(double real) {
  143.         this.real = real;
  144.         this.imag = 0;
  145.     }
  146.  
  147.     public Complex() {
  148.         this(0, 0);
  149.     }
  150.  
  151.     // the constructor takes a String
  152.     public Complex(String str) {
  153.         String newStr = str.trim();
  154.         char[] chars = newStr.toCharArray();
  155.         if (newStr.indexOf("i") == -1)
  156.             this.real = Double.parseDouble(newStr);
  157.         else if (chars.length == 1) {
  158.             this.imag = 1.0;
  159.         } else if (chars.length == 2) {
  160.             this.imag = (double) (chars[0] - '0');
  161.         } else if (chars.length == 3) {
  162.             if (Character.isDigit(chars[0])) {
  163.                 this.real = (double) (chars[0] - '0');
  164.                 this.imag = (chars[1] == '-') ? -1.0 : 1.0;
  165.             } else
  166.                 this.imag = -(double) (chars[1] - '0');
  167.         } else if (chars.length == 4) {
  168.             if (Character.isDigit(chars[0])) {
  169.                 this.real = (double) (chars[0] - '0');
  170.                 this.imag = (chars[1] == '-') ? -(double) (chars[2] - '0') : (double) (chars[2] - '0');
  171.             } else {
  172.                 this.real = -(double) (chars[1] - '0');
  173.                 this.imag = (chars[2] == '-') ? -1.0 : 1.0;
  174.             }
  175.         } else if (chars.length == 5) {
  176.             this.real = -(double) (chars[1] - '0');
  177.             this.imag = (chars[2] == '-') ? -(double) (chars[3] - '0') : (double)(chars[3] - '0');
  178.         }
  179.     }
  180.  
  181.     // define public methods
  182.     public double getReal() {
  183.         return real;
  184.     }
  185.  
  186.     public double getImag() {
  187.         return imag;
  188.     }
  189.  
  190.     public Complex add(Complex c) {
  191.         return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
  192.     }
  193.  
  194.     public Complex subtract(Complex c) {
  195.         return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
  196.     }
  197.  
  198.     public Complex multiply(Complex c) {
  199.         return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
  200.                 (this.real * c.getImag() + c.getImag() * c.getImag()));
  201.     }
  202.  
  203.     public Complex divide(Complex c) {
  204.         double a = this.real;
  205.         double b = this.imag;
  206.         double x = c.getReal();
  207.         double y = c.getImag();
  208.         return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
  209.     }
  210.  
  211.     public double abs() {
  212.         double a = this.getReal();
  213.         double b = this.getImag();
  214.         return Math.sqrt(a * a + b * b);
  215.     }
  216.  
  217.     public Complex negate() {
  218.         double a = this.getReal();
  219.         double b = this.getImag();
  220.         return new Complex(-a, -b);
  221.     }
  222.  
  223.     public Complex conjugate() {
  224.         return new Complex(this.getReal(), -this.getImag());
  225.     }
  226.  
  227.     public double distance(Complex c) {
  228.         double x = this.getReal() - c.getReal();
  229.         double y = this.getImag() - c.getImag();
  230.         return x * x + y * y;
  231.     }
  232.  
  233.     public boolean equals(Complex c) {
  234.         double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
  235.         if (minus / this.abs() < 1E-6)
  236.             return true;
  237.         return false;
  238.     }
  239.  
  240.     public boolean greaterThan(Complex c) {
  241.         return (this.abs() > c.abs()) ? true : false;
  242.     }
  243.  
  244.     public boolean lessThan(Complex c) {
  245.         return !this.greaterThan(c);
  246.     }
  247.  
  248.     public String toString() {
  249.         if (this.getImag() > 0)
  250.             return this.getReal() + "+" + (this.getImag() + "i");
  251.         else if (this.getImag() == 0)
  252.             return this.getReal() + "";
  253.         else
  254.             return this.getReal() + "" + (this.getImag() + "i");
  255.     }
  256.    
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement