### Question:
ComplexNumber { public static ComplexNumber parse(String s) { Objects.requireNonNull(s, "Cannot parse null."); if (s.isBlank()) { throw new IllegalArgumentException("Invalid complex number: blank string."); } String[] parts = s.split("(?=[+-])"); if (parts.length > 2) { throw new IllegalArgumentException("Invalid complex number: too many terms."); } ComplexNumber returnValue = ZERO; boolean hasRealPart, hasImaginaryPart; hasRealPart = hasImaginaryPart = false; for (String part : parts) { ComplexNumber term = parseSingleTerm(part); returnValue = returnValue.add(term); if (Math.abs(term.real) > 0) { if (hasRealPart) { throw new IllegalArgumentException("Invalid complex number: it has two real terms"); } hasRealPart = true; } else if (Math.abs(term.imaginary) > 0) { if (hasImaginaryPart) { throw new IllegalArgumentException("Invalid complex number: it has two imaginary terms"); } hasImaginaryPart = true; } } return returnValue; } 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 testParseWithOnlyI() { assertEquals(ComplexNumber.I, parse("i")); assertEquals(ComplexNumber.MINUS_I, parse("-i")); }
@Test void testParseWithIBeforeImaginaryPart() { assertThrows(IllegalArgumentException.class, () -> parse("i351")); assertThrows(IllegalArgumentException.class, () -> parse("-i317")); assertThrows(IllegalArgumentException.class, () -> parse("i3.51")); assertThrows(IllegalArgumentException.class, () -> parse("-i3.17")); }
@Test void testParseWithRealAndImaginaryPart() { assertEquals(new ComplexNumber(-2.71, -3.15), parse("-2.71-3.15i")); assertEquals(new ComplexNumber(31, 24), parse("31+24i")); assertEquals(new ComplexNumber(-1, -1), parse("-1-i")); }
@Test void testParseWithMultipleSigns() { assertThrows(IllegalArgumentException.class, () -> parse("-+2.71")); assertThrows(IllegalArgumentException.class, () -> parse("--2.71")); assertThrows(IllegalArgumentException.class, () -> parse("-2.71+-3.15i")); }
@Test void testParseWithMultipleComplexNumbers() { assertThrows(IllegalArgumentException.class, () -> parse("2i+3i")); assertThrows(IllegalArgumentException.class, () -> parse("2+3")); assertThrows(IllegalArgumentException.class, () -> parse("1+i-3+i")); }
@Test void testParseWithBlankStrings() { assertThrows(IllegalArgumentException.class, () -> parse("")); assertThrows(IllegalArgumentException.class, () -> parse(" ")); assertThrows(IllegalArgumentException.class, () -> parse(" + ")); }