Commit e67b57be authored by jonathan.craske's avatar jonathan.craske

finished code

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sem2Formative</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
public class Complex
{
double real;
double imag;
double magnitude;
double argument;
public Complex()
{
real = 0;
imag = 0;
}
public Complex(double _real, double _imag)
{
real = _real;
imag = _imag;
}
public double imag()
{
return imag;
}
public double real()
{
return real;
}
public double magnitude()
{
double answer1 = Math.pow(real, 2);
double answer2 = Math.pow(imag, 2);
magnitude = Math.sqrt(answer1 + answer2);
return magnitude;
}
public double argument()
{
argument = Math.atan2(imag, real);
return argument;
}
public Complex add(Complex complex)
{
double answer1 = this.real + complex.real;
double answer2 = this.imag + complex.imag;
complex.real = answer1;
complex.imag = answer2;
return complex;
}
public String toString()
{
if (real == 0.0)
{
String imagString = Double.toString(imag);
return "j" + imagString;
}
if (imag == 0.0)
{
String realString = Double.toString(real);
return realString;
}
if (imag < 0.0)
{
String realString = Double.toString(real);
double num1 = Math.abs(imag);
String imagString = Double.toString(num1);
return realString + " - j" + imagString;
}
else
{
String realString = Double.toString(real);
String imagString = Double.toString(imag);
return realString + " + j" + imagString;
}
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class ComplexTest {
@Test
void testBasic() {
/* Constructor should put the first parameter as the real part and second as the imaginary part.
* real() should return real part, imag() imaginary part.
*/
Complex a = new Complex(4.2, 5.7);
assertEquals(4.2, a.real(), 1e-6);
assertEquals(5.7, a.imag(), 1e-6);
Complex b = new Complex();
assertEquals(0, b.real(), 1e-6);
assertEquals(0, b.imag(), 1e-6);
}
@Test
void testMagnitude() {
/* magnitude() should return the magnitude of the complex number */
Complex a = new Complex(9.6, 11.5);
assertEquals(14.98032042, a.magnitude(), 1e-6);
}
@Test
void testArgument() {
/* argument() should return the argument of the complex number */
Complex a = new Complex(9.6, 11.5);
assertEquals(0.87520335, a.argument(), 1e-6);
}
@Test
void testAdd() {
/* add() should return a new complex number which is the sum of this and the parameter */
Complex a = new Complex(6.6, 12.9);
Complex b = new Complex(13.1, 13.5);
Complex c = a.add(b);
assertEquals(19.7, c.real(), 1e-6);
assertEquals(26.4, c.imag(), 1e-6);
}
@Test
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);
assertEquals("1.2", b.toString());
Complex c = new Complex(0.0, 9.3);
assertEquals("j9.3", c.toString());
Complex d = new Complex(1.2, -3.4);
assertEquals("1.2 - j3.4", d.toString());
}
}
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