-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNum.java
More file actions
71 lines (59 loc) · 1.31 KB
/
ComplexNum.java
File metadata and controls
71 lines (59 loc) · 1.31 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
public class ComplexNum {
private double a;
private double b;
/**
* Creates a new complex number in the form a + bi
* @param a_value real component
* @param b_value imaginary component
*/
public ComplexNum(double a_value, double b_value){
a = a_value;
b = b_value;
}
/**
* Copy constructor
* @param other
*/
public ComplexNum(ComplexNum other){
a = other.a;
b = other.b;
}
//getter methods
public double getReal() {return a;}
public double getImag() {return b;}
/**
* Adds a complex number to this complex number
* @param other
*/
public void add(ComplexNum other){
a += other.a;
b += other.b;
}
/**
* Adds a real
* @param n
*/
public void add(double n){
a += n;
}
public void multiply(ComplexNum other){
double atemp = a*other.a - b*other.b;
b = a*other.b + b*other.a;
a = atemp;
}
public void multiply(int n){
a *= n;
b *= n;
}
public void square(){
double atemp = a*a - b*b;
b = 2*a*b;
a = atemp;
}
public double modulus(){
return Math.sqrt(a*a + b*b);
}
public String toString(){
return a + " + " + b + "i";
}
}