-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplex.java
More file actions
32 lines (28 loc) · 742 Bytes
/
Complex.java
File metadata and controls
32 lines (28 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Complex
{
final private double x;
final private double y;
Complex(double realNum, double imaginaryNum)
{
x = realNum;
y = imaginaryNum;
}
public static Complex add(Complex a, Complex b)
{
return new Complex(a.x+b.x, a.y+b.y);
}
public static Complex multiply(Complex a, Complex b)
{
return new Complex((a.x*b.x - a.y*b.y), (a.x*b.y + a.y*b.x));
}
public static Complex divide(Complex a, Complex b)
{
var v = Math.pow(b.x, 2) + Math.pow(b.y, 2);
return new Complex(((a.x*b.x + a.y*b.y)/ v),
(((a.y*b.x) - (a.x*b.y))/ v));
}
public String toString()
{
return "{" + x + "," + y + "}";
}
}