Mahmoud_Hawara

PA08 - 2

Jan 13th, 2026
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. public class Complex {
  2.     double r;
  3.     double i;
  4.    
  5.     public Complex() {
  6.         r = 0;
  7.         i = 0;
  8.     }
  9.    
  10.     public Complex(double r, double i) {
  11.         this.r = r;
  12.         this.i = i;
  13.     }
  14.    
  15.    
  16.     public double getR() {
  17.         return r;
  18.     }
  19.    
  20.     public double getI() {
  21.         return i;
  22.     }
  23.    
  24.     public void add(Complex cvalue) {
  25.         r += cvalue.r;
  26.         i += cvalue.i;
  27.     }
  28.    
  29.     public static Complex add(Complex cvalue1, Complex cvalue2) {
  30.         return new Complex(cvalue1.r + cvalue2.r, cvalue1.i + cvalue2.i);
  31.     }
  32.    
  33.     public void sub(Complex cvalue) {
  34.         r -= cvalue.r;
  35.         i -= cvalue.i;
  36.     }
  37.  
  38.     public static Complex sub(Complex cvalue1, Complex cvalue2) {
  39.         return new Complex(cvalue1.r - cvalue2.r, cvalue1.i - cvalue2.i);
  40.     }
  41.        
  42.     public String toString() {
  43.         return r + " + " + i + "i";
  44.     }
  45.    
  46.     public static void main(String[] args) {
  47.         Complex x = new Complex(5, 3);
  48.         Complex y = new Complex(2, 8);
  49.        
  50.         System.out.println(x.getR());
  51.         System.out.println(x.getI());
  52.        
  53.         System.out.println(x);
  54.        
  55.         x.add(y);
  56.        
  57.         System.out.println(x);
  58.        
  59.         System.out.println(add(x, y));
  60.        
  61.     }  
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment