Question

In: Computer Science

ASAP (Math: The Complex class) A complex number is a number in the form a +...

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.

Solutions

Expert Solution


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";
      
   }

}


Related Solutions

In java: A complex number is a number in the form a + bi, where a...
In java: 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...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming Exercise from the Book section. If you get a logical or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as...
JAVA Programming A complex number is a number in the form a + bi, where a...
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...
complex number
Find the algebraic form of the following complex number \( (1+i\sqrt{3}) ^{2000} \)
complex number
write polar, carnation,argument, angele , ,and rectangular form of thi Complex number (1+3i)(3+4i)(-5+3i)
Complex number
What are the values of all the cube roots (Z = -4^3 - 4i)?
Final exam scores in a Math class with large number of students have mean 145 and...
Final exam scores in a Math class with large number of students have mean 145 and standard deviation 4.1. Provided the scores of this Final exam follow a normal distribution, what's the probability that a student scores below 145 OR above 157.3? Find the answer without using the LSND program. (Write the answer in decimals)
write a java program to evaluate the highest number input by user, cannot use math class,...
write a java program to evaluate the highest number input by user, cannot use math class, must use overloading . Must compute four computeHighNum methods. Here is what I have. import java.util.Scanner; public class Overload { public static void main(String[] args){ Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); int number3 = input.nextInt();    //int maximum = maximum(number1, number2 ); System.out.printf("Highest integer is ");       // int number1 = input.nextInt(); // int number2 =...
Convert the complex number to polar form rcisθ. (a) For z=−1+i the modulus of zis r=,...
Convert the complex number to polar form rcisθ. (a) For z=−1+i the modulus of zis r=, and the principal argument is θ =. (b) For z=−33‾√−3i the modulus of zis r=, and the principal argument is θ =. (c) For z=−2 the modulus of zis r=, and the principal argument is θ =. (d) For z=−3i the modulus of zis r=, and the principal argument is θ= .
A) Using the general approach (i.e., integrating waves from all sources in complex number form), derive...
A) Using the general approach (i.e., integrating waves from all sources in complex number form), derive the Fraunhofer diffraction amplitude produced by 4 very narrow slits of width "a", each being separated by distance "b". B) Use Babinet's theorem to calculate the Fraunhofer diffraction amplitude of 4 very narrow rectangular plugs of width "a" and separated by "b". Here you do not have to relate the amplitude Einput to the slit diffraction amplitude EL as done in the notes. Consider...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT