Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone help write a JUnit tester for this java complex numbers code. public class ComplexNumber { private float a; private float b; public ComplexNumber(float
Can someone help write a JUnit tester for this java complex numbers code.
public class ComplexNumber { private float a; private float b; public ComplexNumber(float a, float b) { this.a = a; this.b = b; } // 4 methods for add sub mult and div public ComplexNumber add(ComplexNumber c) { ComplexNumber newComplex; float newA = a + c.getA(); float newB = b + c.getB(); newComplex = new ComplexNumber(newA, newB); return newComplex; } public ComplexNumber sub(ComplexNumber c) { ComplexNumber newComplex; float newA = a - c.getA(); float newB = b - c.getB(); newComplex = new ComplexNumber(newA, newB); return newComplex; } public ComplexNumber mult(ComplexNumber c) { ComplexNumber newComplex; float newA = a * c.getA() - b * c.getB(); float newB = b * c.getA() + a * c.getB(); newComplex = new ComplexNumber(newA, newB); return newComplex; } public ComplexNumber div(ComplexNumber c) { ComplexNumber newComplex; float mag = (float) Math.sqrt(c.getA() * c.getA() + c.getB() * c.getB()); float newA = (a * c.getA() + b * c.getB()) / mag; float newB = (b * c.getA() - a * c.getB()) / mag; newComplex = new ComplexNumber(newA, newB); return newComplex; } // getters for A and B public float getA() { return a; } public float getB() { return b; } // equals method public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ComplexNumber that = (ComplexNumber) o; return Float.compare(that.a, a) == 0 && Float.compare(that.b, b) == 0; } // toString() method public String toString() { if (b < 0) { return a + " - " + (-b) + "i"; } else { return a + " + " + (b) + "i"; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started