-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexNumber.java
47 lines (38 loc) · 1.66 KB
/
ComplexNumber.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class ComplexNumber {
private final double real;
private final double imaginary;
public ComplexNumber(final double real, final double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public double getImaginary() {
return this.imaginary;
}
public double getReal() {
return this.real;
}
public ComplexNumber plus(final ComplexNumber other) {
double newReal = this.real + other.real;
double newImaginary = this.imaginary + other.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public ComplexNumber sub(final ComplexNumber other) {
double newReal = this.real - other.real;
double newImaginary = this.imaginary - other.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public ComplexNumber times(final ComplexNumber other) {
double newReal = this.real * other.real;
double newImaginary = this.imaginary * other.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public ComplexNumber div(final ComplexNumber other) {
final double commonDenominator = other.getReal() * other.getReal()
+ other.getImaginary() * other.getImaginary();
final double realNumerator = this.getReal() * other.getReal() + this.getImaginary() * other.getImaginary();
final double imaginaryNumerator = other.getReal() * this.getImaginary() - this.getReal() * other.getImaginary();
return new ComplexNumber(realNumerator / commonDenominator, imaginaryNumerator / commonDenominator);
}
public static void main(String[] args) {
}
}