Answer:
Explanation:
The following code is written in Java, it creates the entire complex class as requested so that it works with the main method that has been provided in the question without having to change anything in the main method. Proof of output can be seen in the attached picture below
import java.util.Scanner;
class Test implements Cloneable{
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 = c1;
System.out.println(c1 == c3);
System.out.println(c3.getRealPart());
System.out.println(c3.getImaginaryPart());
}
}
class Complex implements Cloneable {
public interface Cloneable { }
private double realPart;
public double getRealPart() {
return realPart;
}
public void setReal(double real) {
this.realPart = real;
}
private double imaginaryPart;
public double getImaginaryPart() {
return imaginaryPart;
}
public void setImaginary(double imaginary) {
this.imaginaryPart = imaginary;
}
public Complex(double a, double b) {
realPart = a;
imaginaryPart = b;
}
public Complex(double a) {
realPart = a;
imaginaryPart = 0;
}
public Complex() { }
public Complex add(Complex comp2) {
double real1 = this.getRealPart();
double real2 = comp2.getRealPart();
double imaginary1 = this.getImaginaryPart();
double imaginary2 = comp2.getImaginaryPart();
return new Complex(real1 + real2, imaginary1 + imaginary2);
}
public Complex abs() {
double real1 = Math.abs(this.getRealPart());
double imaginary1 = Math.abs(this.getImaginaryPart());
return new Complex(real1, imaginary1);
}
public Complex subtract(Complex comp2) {
double real1 = this.getRealPart();
double real2 = comp2.getRealPart();
double imaginary1 = this.getRealPart();
double imaginary2 = comp2.getRealPart();
return new Complex(real1 - real2, imaginary1 - imaginary2);
}
public Complex multiply(Complex comp2) {
double real1 = this.getRealPart();
double real2 = comp2.getRealPart();
double imaginary1 = this.getRealPart();
double imaginary2 = comp2.getRealPart();
return new Complex(real1 * real2, imaginary1 * imaginary2);
}
public Complex divide(Complex comp2) {
double real1 = this.getRealPart();
double real2 = comp2.getRealPart();
double imaginary1 = this.getRealPart();
double imaginary2 = comp2.getRealPart();
return new Complex(real1 / real2, imaginary1 / imaginary2);
}
public String toString() {
String result;
result = realPart + " + " + imaginaryPart + "i";
return result;
}
}