1public class ComplexNumber
 2{
 3        private double x,y;
 4        
 5        public ComplexNumber(double real,double imaginary) 
 6        {
 7                this.x=real;
 8                this.y=imaginary;
 9        }

10
11        public double real()
12        {
13            return x; 
14        }

15        
16        public double imaginary()
17        
18            return y; 
19        }

20        
21        public double magnitude()
22        
23            return Math.sqrt(x*x+y*y); 
24        }

25        
26        public String toString()
27        
28            return "{"+x+","+y+"}"
29        }

30        
31        public static ComplexNumber add(ComplexNumber a,ComplexNumber b) 
32        {
33                return new ComplexNumber(a.x+b.x,a.y+b.y);
34        }

35        
36        public ComplexNumber add(ComplexNumber a) 
37        {
38                return new ComplexNumber(this.x+a.x,this.y+a.y);
39        }

40        
41        public static ComplexNumber multiply(ComplexNumber a,ComplexNumber b) 
42        {
43                return new ComplexNumber(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
44        }

45        
46        public ComplexNumber multiply(ComplexNumber a) 
47        {
48                return new ComplexNumber(x*a.x-y*a.y,x*a.y+y*a.x);
49        }

50}