-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
173 lines (135 loc) · 3.05 KB
/
Complex.java
File metadata and controls
173 lines (135 loc) · 3.05 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
Complex - a basic class that represents a complex number a + bi where a and b are integers
@author TODO Your Name
@version TODO Date
@author Period - TODO Your Period
@author Assignment - Complex
@author Sources - TODO list collaborators
*/
public class Complex {
/**
* Constructor
*
* @param r - real component
* @param i - imag component
*/
// TODO
private int real;
private int imag;
public Complex(int r, int i)
{
real = r;
imag = i;
}
public int getReal()
{
return real;
}
public int getImag()
{
return imag;
}
public double magnitude()
{
return Math.sqrt(Math.pow(real,real) + Math.pow(imag,imag));
}
public Complex conjugate()
{
Complex conjugate = new Complex(real, -imag);
return conjugate;
}
public String toString()
{
if(imag == 0) //If just real numebr
{
return "" + real + "";
}
else if (real > 0 && imag > 0) //Both are positive
{
return ""+ real + "+" + imag + "i";
}
else if(real>0 && imag < 0) //The imag part is negative
{
return ""+ real + "-" + imag + "i";
}
else if(real<0 && imag > 0) //The the real is negative
{
return "-"+ real + "+" + imag + "i";
}
else if(real<0 && imag < 0) //Both are negative
{
return "-"+ real + "-" + imag + "i";
}
else if(real ==0)
{
if (imag>0)
{
return "" + imag +"i";
}
else if(imag<0)
{
return "-" + imag +"i";
}
}
return "0";
}
public Complex add(Complex other)
{
int new_real = this.real + other.real;
int new_imag = this.imag + other.imag;
Complex add = new Complex(new_real, new_imag);
return add;
}
public static void main(String[] args)
{
Complex c1 = new Complex(3,4);
Complex c2 = new Complex(6,10);
double m1 = c1.magnitude();
Complex c3 = c2.conjugate();
System.out.println(c1);
System.out.println(c2);
System.out.println(m1);
System.out.println(c3);
}
/**
* Gets the real component
*
* @return real
*/
// TODO
/**
* Gets the real component
*
* @return imag
*/
// TODO
/**
* Calculates the magnitude of the complex number
*
* @return magnutude - sqrt(r^2 + i^2)
*/
// TODO
/**
* Generate the conjugate of the number
*
* @return conjugate
*/
// TODO
/**
* Adds two complex numbers
*
* @return sum of two numbers
*/
// TODO
/**
* For System.out.println() support
*
* Ex. 3 + 4i
* 3
* -5i
* -2 - 6i
*
* @return nicely formatted complex number
*/
// TODO
}