t0xicv4p0r

complex

Feb 16th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package learningjava;
  2.  
  3.  
  4. public class ComplexNumber {
  5.  
  6.     public final static ComplexNumber I = new ComplexNumber(0., 1.);
  7.     public final static ComplexNumber ZERO = new ComplexNumber(0.);
  8.     public final static ComplexNumber ONE = new ComplexNumber(1.);
  9.     private double re;
  10.     private double im;
  11.  
  12.     public ComplexNumber(double re, double im) {
  13.         super();
  14.         this.re = re;
  15.         this.im = im;
  16.     }
  17.  
  18.     public ComplexNumber() {
  19.         this(0., 0.);
  20.     }
  21.  
  22.     public ComplexNumber(ComplexNumber z) {
  23.         this(z.re, z.im);
  24.     }
  25.  
  26.     public ComplexNumber(double re) {
  27.         this(re, 0.);
  28.     }
  29.  
  30.     public double getRe() {
  31.         return this.re;
  32.     }
  33.  
  34.     public void setRe(double re) {
  35.         this.re = re;
  36.     }
  37.  
  38.     public double getIm() {
  39.         return this.im;
  40.     }
  41.  
  42.     public void setIm(double im) {
  43.         this.im = im;
  44.     }
  45.  
  46.     public String toString() {
  47.         if(this.im == 0)
  48.             return "" + this.re;
  49.         return "(" + this.re + ";" + this.im + ")";
  50.     }
  51.  
  52.     public double getModulus() {
  53.         return Math.sqrt(this.re * this.re + this.im * this.im);
  54.     }
  55.  
  56.     public void addTo(ComplexNumber z) {
  57.         this.re += z.re;
  58.         this.im = this.im + z.im;
  59.     }
  60.  
  61.     public ComplexNumber getConjugate() {
  62.         ComplexNumber z = new ComplexNumber(this.re, -this.im);
  63.         return z;
  64.     }
  65.  
  66.     public static ComplexNumber add(ComplexNumber z1, ComplexNumber z2) {
  67.         ComplexNumber z3 = new ComplexNumber(z1.re + z2.re, z1.im + z2.im);
  68.         return z3;
  69.     }
  70.     public ComplexNumber multiplyByC(double c) {
  71.         return new ComplexNumber(this.re * c, this.im * c);
  72.     }
  73.     public static ComplexNumber multiply(ComplexNumber z1, ComplexNumber z2) {
  74.         return new ComplexNumber((z1.re*z2.re) - (z1.im * z2.im), (z1.re*z2.im) + (z1.im*z2.re));
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         ComplexNumber z = new ComplexNumber(1);
  79.         System.out.println(z);
  80.         System.out.println(z.getModulus());
  81.         ComplexNumber z2 = new ComplexNumber(0, 1);
  82.         z.addTo(z2);
  83.         System.out.println(z);
  84.         ComplexNumber z1 = z.getConjugate();
  85.         System.out.println(z1);
  86.  
  87.         ComplexNumber z4 = ComplexNumber.add(z1, z2);
  88.         System.out.println(z4);
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment