Commit 37d91bb4 authored by gavin.white's avatar gavin.white

Upload New File

parent 34611bdf
public class Complex {
public double real;
public double imag;
public Complex() {
real = 2.6;
imag = 5.2;
}
public Complex(double r, double i) {
real = r;
imag = i;
double d = Math.atan2(i, r);
}
public double imag() {
return imag;
}
public double real() {
return real;
}
public double magnitude() {
return Math.sqrt(real * real + imag * imag);
}
public double argument() {
return Math.atan2(imag, real);
}
public String toString() {
// Handle the case where the real part is 0
if (real == 0 && imag != 0) {
return "j" + imag;
}
// Handle the case where the imaginary part is 0
if (imag == 0 && real != 0) {
return real + "";
}
// Handle the case where the imaginary part is negative
if (imag < 0) {
return real + " - j" + (-imag);
}
// Handle the case where both the real and imaginary parts are non-zero
return real + " + j" + imag;
}
public Complex add(Complex other) {
double real = this.real + other.real;
double imag = this.imag + other.imag;
return new Complex(real, imag);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment