-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathComplexNumber.java
More file actions
33 lines (28 loc) · 993 Bytes
/
ComplexNumber.java
File metadata and controls
33 lines (28 loc) · 993 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
33
package com.example.task03;
public class ComplexNumber {
private final double real;
private final double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(
this.real + other.real,
this.imaginary + other.imaginary
);
}
public ComplexNumber multiply(ComplexNumber other) {
double newReal = this.real * other.real - this.imaginary * other.imaginary;
double newImaginary = this.real * other.imaginary + this.imaginary * other.real;
return new ComplexNumber(newReal, newImaginary);
}
@Override
public String toString() {
if (imaginary >= 0) {
return String.format("%.2f + %.2fi", real, imaginary);
} else {
return String.format("%.2f - %.2fi", real, Math.abs(imaginary));
}
}
}