In: Computer Science
ASAP
(Math: The Complex class)
A complex number is a number in the form a + bi, where a and b are
real numbers and i is sqrt( -1). The numbers a and b are known as
the real part and imaginary part of the complex number,
respectively.
You can perform addition, subtraction, multiplication, and division
for complex numbers using the following formulas:
a + bi + c + di = (a + c) + (b + d)i
a + bi - (c + di) = (a - c) + (b - d)i
(a + bi) * (c + di) = (ac - bd) + (bc + ad)i
(a+bi)/(c+di) = (ac+bd)/(c^2 +d^2) + (bc-ad)i/(c^2
+d^2)
You can also obtain the absolute value for a complex number using
the following formula:
| a + bi | = sqrt(a^2 + b^2)
(A complex number can be interpreted as a point on a plane by
identifying the (a,b) values as the coordinates of the point. The
absolute value of the complex number corresponds to the distance of
the point to the origin, as shown in Figure 13.10b.)
Design a class named Complex for representing complex numbers and
the methods add, subtract, multiply, divide, and abs for performing
complex number operations, and override toStringmethod for
returning a string representation for a complex number. The
toString method returns (a + bi) as a string. If b is 0, it simply
returns a. Your Complex class should also implement Cloneable
andComparable. Compare two complex numbers using their absolute
values.
Provide three constructors Complex(a, b), Complex(a), and
Complex(). Complex() creates a Complex object for number 0 and
Complex(a) creates a Complex object with 0 for b. Also provide the
getRealPart() and getImaginaryPart() methods for returning the real
and imaginary part of the complex number, respectively.
Use the code at
https://liveexample.pearsoncmg.com/test/Exercise13_17Test.txt
to test your implementation.
Sample Run
Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
(3.5 + 5.5i) + (-3.5 + 1.0i) = 0.0 + 6.5i
(3.5 + 5.5i) - (-3.5 + 1.0i) = 7.0 + 4.5i
(3.5 + 5.5i) * (-3.5 + 1.0i) = -17.75 -15.75i
(3.5 + 5.5i) / (-3.5 + 1.0i) = -0.5094339622641509 -1.7169811320754718i
|3.5 + 5.5i| = 6.519202405202649
false
3.5
5.5
[-3.5 + 1.0i, 4.0 + -0.5i, 3.5 + 5.5i, 3.5 + 5.5i]
Class Name: Exercise13_17
If you get a logical or runtime error, please refer
https://liveexample.pearsoncmg.com/faq.html.
public class Complex implements Cloneable,
Comparable<Complex> {
private double RealPart;
private double ImaginaryPart;
public Complex(double realPart, double
imaginaryPart) {
RealPart = realPart;
ImaginaryPart =
imaginaryPart;
}
public Complex() {
RealPart = 0;
ImaginaryPart = 0;
}
public Complex(double realPart) {
super();
RealPart = realPart;
ImaginaryPart = 0;
}
public double getRealPart() {
return RealPart;
}
public double getImaginaryPart() {
return ImaginaryPart;
}
public Complex add(Complex num) {
return new
Complex(this.getRealPart()+num.getRealPart(),this.ImaginaryPart+num.ImaginaryPart);
}
public Complex subtract(Complex num) {
return new
Complex(this.getRealPart()-num.getRealPart(),this.ImaginaryPart-num.ImaginaryPart);
}
public Complex multiply(Complex num) {
double a =
this.getRealPart();
double b =
this.getImaginaryPart();
double c = num.getRealPart();
double d =
num.getImaginaryPart();
return new Complex(a*c-b*d,
b*c+a*d);
}
public Complex divide(Complex num) {
double a =
this.getRealPart();
double b =
this.getImaginaryPart();
double c = num.getRealPart();
double d =
num.getImaginaryPart();
return new
Complex((a*c+b*d)/(c*c+d*d), (b*c-a*d)/(c*c+d*d));
}
public double abs() {
return
Math.sqrt(this.RealPart*this.RealPart+this.ImaginaryPart*this.ImaginaryPart);
}
@Override
public int compareTo(Complex num) {
if (this.abs()>num.abs())
return 1;
if (this.abs()<num.abs())
return -1;
return 0;
}
@Override
public Object clone(){
try {
return
super.clone();
} catch (CloneNotSupportedException
e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
if(this.ImaginaryPart==0)
return
this.RealPart+"";
else
if(this.ImaginaryPart<0)
return this.RealPart+"
"+this.ImaginaryPart+"i";
else
return this.RealPart+" +
"+this.ImaginaryPart+"i";
}
}