-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.java
More file actions
106 lines (81 loc) · 2.41 KB
/
Copy pathComplexNumber.java
File metadata and controls
106 lines (81 loc) · 2.41 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
104
105
106
import acm.program.CommandLineProgram;
public class ComplexNumber extends CommandLineProgram {
private double r; // numero real
private double i; // numero complejo
private double mod;
private double arg;
public void sum(ComplexNumber cn){
i += cn.i;
r += cn.r;
this.setMod(r, i);
this.setArg(r, i);
println(this.toString());
}
public void sub(ComplexNumber cn){
i -= cn.i;
r -= cn.r;
this.setMod(r, i);
this.setArg(r, i);
println(this.toString());
}
public void mlt(ComplexNumber cn){
mod *= cn.mod;
arg += cn.arg;
this.setR(mod, arg);
this.setI(mod, arg);
println(this.toString());
}
public void div(ComplexNumber cn){
if(cn.mod != 0){
mod /= cn.mod;
arg -= cn.arg;
this.setR(mod, arg);
this.setI(mod, arg);
println(this.toString());
}else println("Por algún motivo estoy dividiendo por 0, operación no valida ");
}
@Override
public String toString() {
r = roundDecimals(r);
i = roundDecimals(i);
arg = roundDecimals(arg);
mod = roundDecimals(mod);
return ((r>0) ? r : (r<0) ? "- "+r*(-1):"")
+ (i>0.0 ? (" + "+(i)) : (i<0.0) ? " - "+(i*-1) : "")
+("j <=======> ")+mod+" /__ "+arg+"º";
}
private double roundDecimals(double n){ // redondear decimales y limitarlos a 2.
int d = 2; // decimales deseados
return Math.round(n * Math.pow(10, d)) / Math.pow(10, d);
}
public ComplexNumber(double r, double i) {
this.r = r;
this.i = i;
this.setMod(r, i);
this.setArg(r, i);
}
public double getR() {
return r;
}
public double getI() {
return i;
}
public double getMod() {
return mod;
}
public void setMod(double r, double i) {
mod = Math.sqrt(r*r + i*i);
}
public void setR(double m, double a) {
r = m * Math.cos(a);
}
public void setI(double m, double a) {
i = m * Math.sin(a);
}
public double getArg() {
return arg;
}
public void setArg(double r, double i) {
arg = Math.toDegrees(Math.atan(i / r));
}
}