60.7k views
3 votes
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.
Write a test program that prompts the user to enter two complex numbers and display the result of their addition, subtraction, multiplication, and division.
Here is the main class. 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("
}

User Adam Young
by
5.2k points

1 Answer

7 votes

Answer:

Step-by-step 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) = " + 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;

}

}

Design a class named Complex for representing complex numbers and the methods add-example-1
User NamAshena
by
5.0k points