-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathComplexNumber.java
More file actions
39 lines (32 loc) · 1.22 KB
/
ComplexNumber.java
File metadata and controls
39 lines (32 loc) · 1.22 KB
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
package com.example.task03;
public class ComplexNumber {
private final double realPart;
private final double imaginaryPart;
public ComplexNumber(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public double getRealPart() {
return realPart;
}
public double getImaginaryPart() {
return imaginaryPart;
}
public ComplexNumber add(ComplexNumber complexNumber) {
double real = this.realPart + complexNumber.realPart;
double imaginary = this.imaginaryPart + complexNumber.imaginaryPart;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber multiply(ComplexNumber complexNumber) {
double real = this.realPart * complexNumber.realPart - this.imaginaryPart * complexNumber.imaginaryPart;
double imaginary = this.realPart * complexNumber.imaginaryPart + this.imaginaryPart * complexNumber.realPart;
return new ComplexNumber(real, imaginary);
}
@Override
public String toString() {
if (imaginaryPart >= 0) {
return realPart + "+" + imaginaryPart + "i";
}
return realPart + "-" + (-imaginaryPart) + "i";
}
}