How to Create a Class in Java as the Instruction?
Here are the specifications for the Complex class members:
- private double real: stores the real part of the complex number
- private double imaginary: stores the imaginary part of the complex number
Constructors:
- public Complex(double x, double y): initializes a new complex number with the given real and imaginary parts
- public Complex(double x): initializes a new complex number with the given real part and 0 as the imaginary part
- public Complex(): default constructor, initializes a new complex number with 0 as both the real and imaginary parts
Methods:
- public String toString(): returns a string representation of the complex number
- public boolean equals(Complex rhs): returns true if the complex number equals the given complex number, otherwise returns false
- public void setReal(double x): sets the value of the real part to x
- public void setImaginary(double y): sets the value of the imaginary part to y
- public double getReal(): returns the value of the real part
- public double getImaginary(): returns the value of the imaginary part
- public static Complex add(Complex c1, Complex c2): returns a new complex number representing the sum of c1 and c2
- public static Complex subtract(Complex c1, Complex c2): returns a new complex number representing the difference between c1 and c2
- public static Complex multiply(Complex c1, Complex c2): returns a new complex number representing the product of c1 and c2
- public static Complex divide(Complex c1, Complex c2): returns a new complex number representing the division of c1 by c2
The arithmetic operations for complex numbers are defined mathematically as follows:
- Addition: C1 + C2 = (real1 + real2) + (imaginary1 + imaginary2)i
- Subtraction: C1 - C2 = (real1 - real2) + (imaginary1 - imaginary2)i
- Multiplication: C1 * C2 = (real1 * real2) - (imaginary1 * imaginary2) + (real1 * imaginary2 + imaginary1 * real2)i
- Division: C1 / C2 = ((real1 * real2) + (imaginary1 * imaginary2)) / (real2^2 + imaginary2^2) + ((imaginary1 * real2) - (real1 * imaginary2)) / (real2^2 + imaginary2^2)i