Java Reference
In-Depth Information
/** The imaginary part */
private
private
double
double
i
;
/** Construct a Complex */
Complex
(
double
double
rr
,
double
double
ii
) {
r
=
rr
;
i
=
ii
;
}
/** Display the current Complex as a String, for use in
* println() and elsewhere.
*/
public
public
String
toString
() {
StringBuffer sb
=
new
new
StringBuffer
().
append
(
r
);
iif
(
i
>
0
)
sb
.
append
(
'+'
);
// else append(i) appends - sign
return
return
sb
.
append
(
i
).
append
(
'i'
).
toString
();
}
/** Return just the Real part */
public
public
double
double
getReal
() {
return
return
r
;
}
/** Return just the Real part */
public
public
double
double
getImaginary
() {
return
return
i
;
}
/** Return the magnitude of a complex number */
public
public
double
double
magnitude
() {
return
return
Math
.
sqrt
(
r
*
r
+
i
*
i
);
}
/** Add another Complex to this one
*/
public
public
Complex
add
(
Complex other
) {
return
return
add
(
this
this
,
other
);
}
/** Add two Complexes
*/
public
public static
static
Complex
add
(
Complex c1
,
Complex c2
) {
return
return new
new
Complex
(
c1
.
r
+
c2
.
r
,
c1
.
i
+
c2
.
i
);
}
/** Subtract another Complex from this one
*/