Commit 3608fa81 authored by quinn.haigh's avatar quinn.haigh

second commit

parent eeeee93c
public class Complex
{
double real;
double imag;
public Complex()
{
real = 0;
imag = 0;
}
public Complex(double _real, double _imag)
{
real = _real;
imag = _imag;
}
public double real()
{
return real;
}
public double imag()
{
return imag;
}
public double magnitude()
{
double answerA = Math.pow(real, 2);
double answerB = Math.pow(imag, 2);
double result = answerA + answerB;
double mag = Math.sqrt(result);
return mag;
}
public double argument()
{
double arg = Math.atan2(imag, real);
return arg;
}
public Complex add(Complex b)
{
Complex a = this;
double ansA = a.real + b.real;
double ansB = a.imag + b.imag;
return new Complex(ansA, ansB);
}
public String toString()
{
if(real == 0.0)
{
String stringI = Double.toString(imag);
return "j" + stringI;
}
if(imag == 0.0)
{
String stringR = Double.toString(real);
return stringR;
}
if(imag < 0.0)
{
String stringR = Double.toString(real);
double negate = Math.abs(imag);
String stringI = Double.toString(negate);
return stringR + " - " + "j" + stringI;
}
else
{
String stringR = Double.toString(real);
String stringI = Double.toString(imag);
return stringR + " + " + "j" + stringI;
}
}
}
......@@ -45,7 +45,7 @@ class ComplexTest {
void testToString() {
/* toString() should return a string like 1.2 + j3.4 where 1.2 is the real part
* and 3.4 the imaginary part.
*
*/
Complex a = new Complex(1.2, 3.4);
assertEquals("1.2 + j3.4", a.toString());
Complex b = new Complex(1.2, 0.0);
......
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