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
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 aand 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 bis 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
fix code: 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)); } } class Complex implements Cloneable, Comparable
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"; } }
contains error: Exercise13_17.java:40: error: class, interface, or enum expected import java.util.Scanner;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
