A complex number a + bi has a real part a and an imaginary part b. Write an immutable class Complex implements Comparable<Complex> and a public class ComplexApp.
- Constructors
Complex(re, im)andComplex(re); accessorsgetRealPart(),getImaginaryPart(). add,subtract,multiply,divide— each returns a newComplex: (a + bi)(c + di) = (ac − bd) + (bc + ad)i and (a + bi)/(c + di) = ((ac + bd) + (bc − ad)i) / (c² + d²).abs()= √(a² + b²), the distance from the origin.compareToorders complex numbers by their absolute value.toString():3.50 + 5.50i,3.50 - 5.50i, or just3.50when the imaginary part is 0.
main reads a b c d. For the input
3.5 5.5 -3.5 1
the program prints
(3.50 + 5.50i) + (-3.50 + 1.00i) = 0.00 + 6.50i
(3.50 + 5.50i) - (-3.50 + 1.00i) = 7.00 + 4.50i
(3.50 + 5.50i) * (-3.50 + 1.00i) = -17.75 - 15.75i
(3.50 + 5.50i) / (-3.50 + 1.00i) = -0.51 - 1.72i
|3.50 + 5.50i| = 6.519 and |-3.50 + 1.00i| = 3.640
The second is closer to the origin