Question
PLEASE HELP Can you fix my code please? this is what I am suppose to do: Design a class named Complex for representing complex numbers
PLEASE HELP
Can you fix my code please?
this is what I am suppose to do:
Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, abs for performing complex-number operations, and override 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. 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. Your Complex class should also implement the Cloneable interface.
This is suppose to be the output:
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.5094 + -1.7i |3.5 + 5.5i| = 6.519202405202649
This is my Complex class:
public class Complex implements Cloneable { private double a; private double b; public Complex(double a,double b) { this.a = a; this.b = b; } public Complex(double a) { this.a = a; b = 0; } public Complex() { this.a = 0; this. b =0; } public double getRealPart() { return a; } public double getImaginaryPart() { return b; } @Override public String toString() { return "(" + a + "+i" + b + "}"; } public Complex add(Complex com1) { return new Complex(com1.getRealPart() + a, com1.getImaginaryPart() + b); } public Complex subtract(Complex com1) { return new Complex(com1.getRealPart() - a, com1.getImaginaryPart() - b); } public Complex multiply(Complex com1) { return new Complex(com1.getRealPart() * a, com1.getImaginaryPart() * b); } public Complex divide(Complex com1) { return new Complex(com1.getRealPart() / a, com1.getImaginaryPart() / b); } public Complex abs() { return new Complex (Math.abs(a), Math.abs(b)); } @Override protected Object clone() { return new Complex(a,b); } }
this is the main class and it cannot be changed:
import java.util.Scanner; public class Test { 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()); } } This is what I am getting for the output:
Enter the first complex number: 3.5 5.5 Enter the second complex number: -3.5 1 ((3.5+i5.5}) + ((-3.5+i1.0}) = (0.0+i6.5} ((3.5+i5.5}) - ((-3.5+i1.0}) = (-7.0+i-4.5} ((3.5+i5.5}) * ((-3.5+i1.0}) = (-12.25+i5.5} ((3.5+i5.5}) / ((-3.5+i1.0}) = (-1.0+i0.18181818181818182} |(3.5+i5.5}| = (3.5+i5.5} false 3.5 5.5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started