-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
103 lines (81 loc) · 2.45 KB
/
Copy pathComplex.java
File metadata and controls
103 lines (81 loc) · 2.45 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package T1;
public class Complex {
private double real = 0.0;
private double imag = 0.0;
private final static double EPSILON = 1e-8;
public Complex(){
}
public Complex(double real,double imag) {
if(Double.isNaN(real) || Double.isNaN(imag)){
System.out.println("You can not type in NaN");
}
else{
this.real = real;
this.imag = imag;
}
}
public double getReal(){
return this.real;
}
public void setReal(double real){
this.real = real;
}
public double getImag(){
return this.imag;
}
public void setImag(double imag){
this.imag = imag;
}
public void setValue(double real,double imag){
this.real = real;
this.imag = imag;
}
public String toString(){
String print =String.format("%f+%fi",this.real,this.imag);
return print;
}
public boolean isReal(){
if(this.imag == 0){
return true;
}
else{
return false;
}
}
public boolean isImaginary(){
if(isReal() == false && this.real == 0)
{
return true;
}
else{
return false;
}
}
public boolean equals(double real,double imag){
if((real-this.real)*(real-this.real)<EPSILON*EPSILON && (imag-this.imag)*(imag-this.imag)<EPSILON*EPSILON){
return true;
}
else{
return false;
}
}
public boolean equals(Complex another){
return equals(another.real,another.imag);
}
public double abs()
{
return Math.sqrt(this.real*this.real+this.imag*this.imag);
}
public Complex add(Complex right){
return new Complex(right.real+this.real,right.imag+this.imag);
}
public Complex subtract(Complex right){
return new Complex(this.real-right.real,this.imag-right.imag);
}
public Complex multiply(Complex right){
return new Complex(this.real*right.real-this.imag*right.imag,this.real*right.imag+this.imag*right.real);
}
public Complex divide(Complex right){
return new Complex((this.real*right.real+this.imag*right.imag)/(right.real*right.real+right.imag*right.imag),(-this.real*right.imag+this.imag*right.real)/(right.real*right.real+right.imag*right.imag));
}
}