-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.java
More file actions
51 lines (47 loc) · 1.53 KB
/
Copy pathComplexNumber.java
File metadata and controls
51 lines (47 loc) · 1.53 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
40
41
42
43
44
45
46
47
48
49
50
51
public class ComplexNumber {
private int real, imag;
public ComplexNumber(int real, int imag) {
this.real = real;
this.imag = imag;
}
public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(this.real + other.real, this.imag + other.imag);
}
public ComplexNumber sub(ComplexNumber other) {
return new ComplexNumber(this.real - other.real, this.imag - other.imag);
}
public ComplexNumber mult(ComplexNumber other) {
int multReal = this.real * other.real - this.imag * other.imag;
int multImag = this.real * other.imag + other.real * this.imag;
return new ComplexNumber(multReal, multImag);
}
public void print_elem() {
if (real == 0 && imag == 0) {
System.out.print(0);
}
else if (real == 0) {
if (imag == 1) {
System.out.print("i");
}
else if (imag == -1) {
System.out.print("-i");
}
else System.out.print(imag + "i");
}
else {
if (imag < 0) {
if (imag == -1) {
System.out.print(real + " - i");
}
else System.out.print(real + " - " + (-imag) + "i");
}
else if (imag == 0) {
System.out.print(real);
}
else if (imag == 1) {
System.out.print(real + " + i");
}
else System.out.print(real + " + " + imag + "i");
}
}
}