### Question:
ComplexNumber { public ComplexNumber power(int n) { if (n < 0) { throw new IllegalArgumentException("Cannot calculate power with a negative exponent."); } else if (n == 0) { return ONE; } double magnitude = Math.pow(getMagnitude(), n); double angle = n * getAngle(); return fromMagnitudeAndAngle(magnitude, angle); } 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 testPowerWithOne() { for (int n = 0; n < 10; ++n) { assertEquals(ComplexNumber.ONE, ComplexNumber.ONE.power(n)); } }
@Test void testPowerWithKnownNumbers() { assertEquals(ComplexNumber.MINUS_I, ComplexNumber.I.power(163)); assertEquals(new ComplexNumber(-506864, 1236480), new ComplexNumber(3, 5).power(8)); }
@Test void testPowerWithZeroAsExponent() { for (int index = 0; index < numbers.length; index++) { assertEquals(ComplexNumber.ONE, numbers[index].power(0)); } }
@Test void testPowerWithNegativeExponent() { assertThrows(IllegalArgumentException.class, () -> ComplexNumber.MINUS_ONE.power(-1)); }