Question

In: Computer Science

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



Class Name: Exercise13_17

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

Let me know for any help with any other questions.

Thank You! 
===========================================================================



import java.util.Scanner;

public class Exercise13_17 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first complex number: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        Complex c1 = new Complex(a, b);

        System.out.print("Enter the second complex number: ");
        double c = input.nextDouble();
        double d = input.nextDouble();
        Complex c2 = new Complex(c, d);

        System.out.println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = " + c1.add(c2));
        System.out.println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = " + c1.subtract(c2));
        System.out.println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = " + c1.multiply(c2));
        System.out.println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = " + c1.divide(c2));
        System.out.println("|" + c1 + "| = " + c1.abs());

        Complex c3 = null;
        try {
            c3 = (Complex) c1.clone();
        } catch (CloneNotSupportedException e) {

        }
        System.out.println(c1 == c3);
        System.out.println(c3.getRealPart());
        System.out.println(c3.getImaginaryPart());
        Complex c4 = new Complex(4, -0.5);
        Complex[] list = {c1, c2, c3, c4};
        java.util.Arrays.sort(list);
        System.out.println(java.util.Arrays.toString(list));
    }
}

// BEGIN REVEL SUBMISSION
// Write your code
class Complex implements Cloneable, Comparable<Complex> {
    private double a;
    private double b;

    public Complex(double a, double b) {
        this.a = a;
        this.b = b;
    }


    public int compareTo(Complex o) {
        return Double.compare(abs(),o.abs());
    }

    public Complex add(Complex c2) {

        double aSum = a + c2.a;
        double bSum = b + c2.b;
        return new Complex(aSum, bSum);
    }

    public Complex subtract(Complex c2) {
        double aSub = a - c2.a;
        double bSub = b - c2.b;
        return new Complex(aSub, bSub);
    }

    public Complex multiply(Complex c2) {

        double aMul = (a * c2.a - b * c2.b);
        double bMul = (b * c2.a + a * c2.b);
        return new Complex(aMul, bMul);
    }

    public Complex divide(Complex c2) {
        double div = c2.a * c2.a + c2.b * c2.b;
        double aDiv = (a * c2.a + b * c2.b) / div;
        double bDiv = (b * c2.a - a * c2.b) / div;

        return new Complex(aDiv, bDiv);

    }

    public double abs() {
        return Math.sqrt(a * a + b * b);
    }

    public double getRealPart() {
        return a;
    }

    public double getImaginaryPart() {
        return b;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new Complex(a, b);
    }

    @Override
    public String toString() {
        return "" + a + " + " + b + "i";
    }
}
// END REVEL SUBMISSION

=======================================================================


Related Solutions

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...
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 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....
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)?
Complex Numbers: What's the difference between Log(z), log(z) and ln(z)? where z is a complex number,...
Complex Numbers: What's the difference between Log(z), log(z) and ln(z)? where z is a complex number, z = x + iy
A valid IPv4 address is of the form ###.###.###.###, where each number, separated by '.', is...
A valid IPv4 address is of the form ###.###.###.###, where each number, separated by '.', is 8 bits [0, 255]. Create program that validates input string containing an IP address. Use C programming language. - Remove any newline \n from input string - The input prompt should say "Enter IP Address of your choosing: " - If IP address is invalid, it should print "You entered invalid IP\n" - If IP address is valid, it should print "You entered valid...
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT