binibiningtinamoran

ComplexNumbers

Oct 29th, 2019
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. public class Complex {
  2.  
  3.     private final double real;
  4.     private final double imaginary;
  5.  
  6.     /** A convenience method, shorter than a constructor call, for
  7.      * creating complex numbers.
  8.      * @param real The real component of the number.
  9.      * @param imaginary The imaginary component of the number.
  10.      * @return An immutable instance of <code>Complex</code>
  11.      * representing the indicated number.
  12.      */
  13.     public Complex(double real, double imaginary) {
  14.         this.real = real;
  15.         this.imaginary = imaginary;
  16.     }
  17.  
  18.     /** The real component of this number. */
  19.     public double getReal() {
  20.         return this.real;
  21.     }
  22.  
  23.     /** The imaginary component of this number. */
  24.     public double getImaginary(){
  25.         return this.imaginary;
  26.     }
  27.  
  28.     public boolean isZero(){
  29.         return this.real == 0 && this.imaginary == 0;
  30.     }
  31.  
  32.     public double getMagnitude(){
  33.         return Math.hypot(real, imaginary);
  34.     }
  35.  
  36.     public Complex add(Complex that){
  37.         Complex a = this;
  38.         double real = a.real + that.real; // or a.getReal() + that.getReal();
  39.         double imaginary = a.getImaginary() + that.getImaginary();
  40.         return new Complex(real, imaginary);
  41.     }
  42.  
  43.     public Complex subtract(Complex that){
  44.         Complex a = this;
  45.         double real = a.real - that.real;
  46.         double imaginary = a.getImaginary() - that.getImaginary();
  47.         return new Complex(real, imaginary);
  48.     }
  49.  
  50.     /** Returns the product of this number and <code>that</code>.  Does
  51.      * not change either <code>this</code> or the argument.  We use the
  52.      * standard definition of complex arithmetic; see for example
  53.      * http://mathworld.wolfram.com/ComplexNumber.html .  */
  54.     public Complex multiply(Complex that){
  55.         Complex a = this;
  56.         double real = (a.real * that.real) - (a.imaginary * that.imaginary);
  57.         double imaginary =
  58.                 (a.getReal() * that.getImaginary()) + (a.getImaginary() * that.getReal());
  59.         return new Complex(real, imaginary);
  60.     }
  61.  
  62.     /** Returns the quotient of this number over <code>that</code>.
  63.      * Does not change either <code>this</code> or the argument.  We use
  64.      * the standard definition of complex arithmetic; see for example
  65.      * http://mathworld.wolfram.com/ComplexNumber.html .  */
  66.     public Complex dividedBy(Complex that){
  67.         // FILL IN
  68.         // Multiply the expression by the conjugate of the divisor/denominator!!!!
  69.  
  70.         return this;
  71.  
  72.     }
  73.  
  74.     public Complex conjugate() {
  75.         return new Complex(real, -imaginary); // CALL THIS METHOD IN THE dividedBy() method!
  76.     }
  77.     public Complex reciprocal() {
  78.         return this; // FILL IN THEN CALL THIS METHOD IN THE dividedBy() method!
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment