Question

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]

so I did like this:

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 = (Complex) c1.clone();
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
class Complex implements Cloneable, Comparable<Complex>{
private double re;
private double im;
//******************************

// Constructor 1
public Complex(double real, double imag) {
re = real;
im = imag;
}
  
// Constructor 2
public Complex(double real) {
re = real;
im = 0;
}

// Constructor 3
public Complex() {
re = 0;
im = 0;
}

public double abs() {
return Math.sqrt(re * re + im * im);
}

//**********************************
// get method for real part
public double getRealPart() {
return this.re;
}

// get method for imaginary part
public double getImaginaryPart() {
return this.im;
}

@Override
public String toString() {
if(im < 0) {
return re + " " + im + "i";
}
return re + " + " + im + "i";
}

public Complex add(Complex temp) {
Complex result = new Complex();
result.re = this.getRealPart() + temp.getRealPart();
result.im = this.getImaginaryPart() + temp.getImaginaryPart();
return result;
}

public Complex subtract(Complex temp) {
Complex result = new Complex();
result.re = this.getRealPart() - temp.getRealPart();
result.im = this.getImaginaryPart() - temp.getImaginaryPart();
return result;
}

public Complex multiply(Complex temp) {
Complex result = new Complex();
result.re = (this.getRealPart() * temp.getRealPart()) - (this.getImaginaryPart() * temp.getImaginaryPart());
result.im = (this.getRealPart() * temp.getImaginaryPart()) + (this.getImaginaryPart() * temp.getRealPart());
return result;
}

public Complex divide(Complex temp) {
Complex result = new Complex();
result.re = (((this.getRealPart() * temp.getRealPart()) + (this.getImaginaryPart() * temp.getImaginaryPart()))
/ (Math.pow(temp.getRealPart(), 2) + Math.pow(temp.getImaginaryPart(), 2)));
result.im = (((this.getRealPart() * temp.getImaginaryPart()) - (this.getImaginaryPart() * temp.getRealPart()))
/ (Math.pow(temp.getRealPart(), 2) + Math.pow(temp.getImaginaryPart(), 2)));
return result;
}

@Override
public int compareTo(Complex o) {
return (int) (abs() - o.abs());
}
  
public Object clone() {
return new Complex(getRealPart(), getImaginaryPart());
}
}

but I got the result:

Input for the Test 34 23 23 1.4 Your Output Enter the first complex number: 34.0 23.0 Enter the second complex number: 23.0 1The Correct Output The correct output should contain the following substrings in this order, separated by the # sig ns. Note

I would like to get + 0.9066596353774294i instead of -0.9066596353774294i.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<terminated > Exercise 13_17 [Java Application] C:\Program Files\Java\jdk1.8.0_251\bin\javaw.exe (Jul 27, 2020, 1: Enter the

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 = (Complex) c1.clone();
        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
class Complex implements Cloneable, Comparable<Complex>{
    private double re;
    private double im;
    //******************************  

    // Constructor 1
    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }
    
    // Constructor 2
    public Complex(double real) {
        re = real;
        im = 0;
    }

    // Constructor 3
    public Complex() {
        re = 0;
        im = 0;
    }

    public double abs() {
        return Math.sqrt(re * re + im * im);
    }

    //**********************************
    // get method for real part
    public double getRealPart() {
        return this.re;
    }

    // get method for imaginary part
    public double getImaginaryPart() {
        return this.im;
    }

    @Override
    public String toString() {
        if(im < 0) {
            return re + " " + im + "i";
        }
        return re + " + " + im + "i";
    }

    public Complex add(Complex temp) {
        Complex result = new Complex();
        result.re = this.getRealPart() + temp.getRealPart();
        result.im = this.getImaginaryPart() + temp.getImaginaryPart();
        return result;
    }

    public Complex subtract(Complex temp) {
        Complex result = new Complex();
        result.re = this.getRealPart() - temp.getRealPart();
        result.im = this.getImaginaryPart() - temp.getImaginaryPart();
        return result;
    }

    public Complex multiply(Complex temp) {
        Complex result = new Complex();
        result.re = (this.getRealPart() * temp.getRealPart()) - (this.getImaginaryPart() * temp.getImaginaryPart());
        result.im = (this.getRealPart() * temp.getImaginaryPart()) + (this.getImaginaryPart() * temp.getRealPart());
        return result;
    }
    
    private Complex reciprocal() {
        double scale = re*re + im*im;
        return new Complex(re / scale, -im / scale);
    }


    public Complex divide(Complex temp) {
        return this.multiply(temp.reciprocal());
    }


    @Override
    public int compareTo(Complex o) {
        return (int) (abs() - o.abs());
    }
    
    public Object clone() {
        return new Complex(getRealPart(), getImaginaryPart());
    }
}
// END REVEL SUBMISSION

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
A complex number is a number in the form a + bi, where a and b...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 V-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 + di a + bi - (c +...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (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...

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

  • Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public...

    Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public class Complex extends Number [ public Complex(double r, double i) public Complex add(Number n) [ Complex that - (Complex) n; return new Complex(this.re+that.re, this.im+that.im); private double re; private double im; public class Real extends Number public Real(double v) val-v public Real add(Number n) Real that Rea) n; return new Real(this.val+that.val) private double val; Unsurprisingly, the following code compiles. Number n- new Complex(2,3).add(new Real(1)); What...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • 2. A complex number can be expressed as a + bi where a and b are...

    2. A complex number can be expressed as a + bi where a and b are real numbers and i is the imaginary unit. The multiplication of two complex numbers is defined as follows: (a+bi)(c+di) = (ac-bd) + (bc+ad)i Define a class which represents a complex number. The only member functions you have to define and implement are those which overload the * and *= symbols.

  • For the complex number given as: z = a + bi / c+di where i =...

    For the complex number given as: z = a + bi / c+di where i = √−1 is the imaginary unit. The parameters are defined as a = √2, b = 0, c = 0.5 and d = −0.5. (a) Find the real and the imaginary parts of z, and then draw the Argand dia- gram. (Hint: Use the conjugate of the denominator.) 2.5 (b) Based on the Argand diagram, find the distance r of the complex number z from...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT