Advertisement
STANAANDREY

complex java

Oct 2nd, 2023 (edited)
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. class Complex {
  2.     private static long cnt = 0;
  3.     private double re, img;
  4.     public Complex(double re, double img) {
  5.         this.re = re;
  6.         this.img = img;
  7.     }
  8.     public double abs() {
  9.         return Math.sqrt(re * re + img * img);
  10.     }
  11.     public void print() {
  12.         System.out.println(re + " + " + img + "i");
  13.         Complex.cnt++;
  14.     }
  15.     public Complex add(final Complex other) {
  16.         return new Complex(re + other.re, img + other.img);
  17.     }
  18.     public static long getCnt() {
  19.         return Complex.cnt;
  20.     }
  21. }
  22.  
  23. class ClientComplex {
  24.     public static void main(String argv[]) {
  25.         Complex a = new Complex(3, -2);
  26.         Complex b = new Complex(3, 4);
  27.         a.print();
  28.         a.add(b).print();
  29.         System.out.println(b.abs());
  30.         System.out.println(Complex.getCnt());
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement