In: Computer Science
JAVA Programming
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.10.)
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 the toString method 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 and
Comparable. 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_17.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]
The Output must be similar to the Sample Run
The Class Name MUST be Exercise13_17
If you get a logical or runtime error, please refer
https://liveexample.pearsoncmg.com/faq.html.
Please find my implementation.
public class ComplexNumber {
private double real;
private double imag;
// default constructor
public ComplexNumber() {
real = imag = 0;
}
// parameterized constructor
public ComplexNumber(double r, double i) {
real = r;
imag = i;
}
// parameterized constructor
public ComplexNumber(double a) {
real = a;
imag = 0;
}
// copy constructor
public ComplexNumber(ComplexNumber other){
real = other.real;
imag= other.imag;
}
public double getReal() {
return real;
}
public double getImaginary() {
return imag;
}
// toString method
public String toString() {
return "("+String.format("%.2f", real)+" + "+String.format("%.2f", imag)+"i)";
}
// function to add
public ComplexNumber add(ComplexNumber other){
ComplexNumber sum = new ComplexNumber();
sum.real = real + other.real;
sum.imag = imag + other.imag;
return sum;
}
// function to suntract
public ComplexNumber subtract(ComplexNumber other){
ComplexNumber sub = new ComplexNumber();
sub.real = real - other.real;
sub.imag = imag - other.imag;
return sub;
}
// function to multiply
public ComplexNumber multiplyBy(ComplexNumber other){
ComplexNumber mul = new ComplexNumber();
mul.real = real*other.real + imag*other.imag;
mul.imag = imag*other.real + other.imag*real;
return mul;
}
// function to divide
public ComplexNumber divideBy(ComplexNumber other){
if(other.real == 0 && other.imag == 0)
throw new IllegalArgumentException("Both ral and imaginary part of denominator can not be zero");
ComplexNumber divide = new ComplexNumber();
divide.real=((real*other.real)+(imag*other.imag))/((other.real*other.real)+(other.imag*other.imag));
divide.imag=((imag*other.real)-(real*other.imag))/((other.real*other.real)+(other.imag*other.imag));
return divide;
}
public double abs(){
return Math.sqrt(real*real + imag*imag);
}
public boolean isRealPart(){
return imag ==0;
}
public boolean isImaginaryPart(){
return real == 0;
}
}
###########
import java.util.Scanner;
public class ComplexNumberTesting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first complex number: ");
double r = sc.nextDouble();
double img = sc.nextDouble();
ComplexNumber c1 =new ComplexNumber(r,img);
System.out.print("Enter the second complex number: ");
r = sc.nextDouble();
img = sc.nextDouble();
sc.close();
ComplexNumber c2 = new ComplexNumber(r,img);
System.out.println("C1: "+c1);
System.out.println("C2: "+c2);
ComplexNumber sum = c1.add(c2);
System.out.println(c1+"+"+c2+" = " +sum);
ComplexNumber sub = c1.subtract(c2);
System.out.println(c1+"-"+c2+" = " +sub);
ComplexNumber mul = c1.multiplyBy(c2);
System.out.println(c1+"*"+c2+" = " +mul);
ComplexNumber divide = c1.divideBy(c2);
System.out.println(c1+"/"+c2+" = " +divide);
System.out.println("|"+c1+"| = "+c1.abs());
}
}
/*
Sample Output:
Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
C1: (3.50 + 5.50i)
C2: (-3.50 + 1.00i)
(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) = (-6.75 + -15.75i)
(3.50 + 5.50i)/(-3.50 + 1.00i) = (-0.51 + -1.72i)
|(3.50 + 5.50i)| = 6.519202405202649
*/