### Question:
ComplexNumber { public ComplexNumber[] root(int n) { if (n <= 0) { throw new IllegalArgumentException("Cannot calculate negative roots."); } double magnitude = Math.pow(getMagnitude(), 1.0 / n); double angle = getAngle() / n; double angleStep = 2 * Math.PI / n; ComplexNumber[] roots = new ComplexNumber[n]; for (int index = 0; index < n; ++index) { roots[index] = fromMagnitudeAndAngle(magnitude, angle + index * angleStep); } return roots; } ComplexNumber(double real, double imaginary); static ComplexNumber fromReal(double real); static ComplexNumber fromImaginary(double imaginary); static ComplexNumber fromMagnitudeAndAngle(double magnitude, double angle); static ComplexNumber parse(String s); double getReal(); double getImaginary(); double getMagnitude(); double getAngle(); ComplexNumber conjugate(); ComplexNumber add(ComplexNumber c); ComplexNumber sub(ComplexNumber c); ComplexNumber mul(double d); ComplexNumber mul(ComplexNumber c); ComplexNumber div(double d); ComplexNumber div(ComplexNumber c); ComplexNumber power(int n); ComplexNumber[] root(int n); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static final ComplexNumber ONE; static final ComplexNumber MINUS_ONE; static final ComplexNumber ZERO; static final ComplexNumber I; static final ComplexNumber MINUS_I; static final double THRESHOLD; }### Answer:
@Test void testFirstRoot() { for (int index = 0; index < numbers.length; index++) { assertEquals(numbers[index], numbers[index].root(1)[0]); } }
@Test void testNegativeRoot() { assertThrows(IllegalArgumentException.class, () -> ComplexNumber.MINUS_ONE.root(-1)); }
@Test void testZeroRoot() { assertThrows(IllegalArgumentException.class, () -> ComplexNumber.MINUS_ONE.root(0)); }
@Test void testKnownRoots() { assertArrayEquals(new ComplexNumber[] { ComplexNumber.I, ComplexNumber.MINUS_I }, ComplexNumber.MINUS_ONE.root(2)); assertArrayEquals(new ComplexNumber[] { ComplexNumber.ONE, ComplexNumber.I, ComplexNumber.MINUS_ONE, ComplexNumber.MINUS_I }, ComplexNumber.ONE.root(4)); assertArrayEquals(new ComplexNumber[] { new ComplexNumber(1.05857815, 0.16766230), new ComplexNumber(0.16766230, 1.05857815), new ComplexNumber(-0.95495715, 0.48657496), new ComplexNumber(-0.75785828, -0.75785828), new ComplexNumber(0.48657496, -0.9549571) }, new ComplexNumber(1, 1).root(5)); }