Question

In: Computer Science

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 + 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.

Solutions

Expert Solution

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

*/


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...
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 +...
In Matlab: Any complex number z=a+bi can be given by its polar coordinates r and θ,...
In Matlab: Any complex number z=a+bi can be given by its polar coordinates r and θ, where r=|z|=sqrt(a^2+b^2) is the magnitude and θ= arctan(ba) is the angle. Write a function that will return both the magnitude r and the angle θ of a given complex numberz=a+bi. You should not use the built-in functions abs and angle. You may use the built-in functions real and imag.
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
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)?
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT