
Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case no initializers are provided. 3- Provide a constructor that enables an object of this class to be initialized when it is declared. 4- Provide public operations that perform the following : a) Add two Complex numbers. b) Subtract two Complex numbers. c) Multiply two Complex numbers. d) Divide two Complex numbers. e) Print Complex numbers in the form (a + i b), where a is the real part and b is the imaginary part.

package sampl1;
public class Complex {
double real;
double img;
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImg() {
return img;
}
public void setImg(double img) {
this.img = img;
}
public Complex() {
real=1;
img=2;
}
public Complex(double img, double real) {
super();
this.img = img;
this.real = real;
}
public Complex addComplex(Complex c2)
{
Complex c3=new Complex();
c3.real=real+c2.real;
c3.img=img+c2.img;
return c3;
}
public Complex subComplex(Complex c2)
{
Complex c3=new Complex();
c3.real=real-c2.real;
c3.img=img-c2.img;
return c3;
}
public Complex mulComplex(Complex c2)
{
Complex c3=new Complex();
c3.real=((real*c2.real)-(c2.img * img));
c3.real=((real*c2.img)+(c2.real*img));
return c3;
}
public Complex divComplex(Complex c2)
{
Complex c3=new Complex();
c3.real=((real*c2.real) - (img*c2.img))/((c2.real * c2.real) - (c2.img* c2.img));
c3.img=((real*c2.img)+(c2.real*img))/((c2.real*c2.real) - (c2.img*c2.img));
return c3;
}
public void showComplex()
{
System.out.println("The Complex number is: " + real +"+i"+img);
}
}

i write this programe me but when i run the programe it dosn't make the calculation (add, subtract...ect) could any one have a look this programe and tell me where is the error? this programe written in two class (one with constractor and metheod and one with the main)
*this is the first class.
public class Complex {
private double real;
private double img;
public Complex(){
}
public double getReal()
{
return real;
}
public void setReal(double real)
{
this.real = real;
}
public double getImg()
{
return img;
}
public void setImg(double img)
{
this.img = img;
}
public Complex(double img, double real)
{
super();
this.img = img;
this.real = real;
}
public Complex add(Complex c2)
{
Complex c3=new Complex();
c3.real=real+c2.real;
c3.img=img+c2.img;
return c3;
}
public Complex subtract(Complex c2)
{
Complex c3=new Complex();
c3.real=real-c2.real;
c3.img=img-c2.img;
return c3;
}
public Complex multiply(Complex c2)
{
Complex c3=new Complex();
c3.real=((real*c2.real)-(c2.img * img));
c3.real=((real*c2.img)+(c2.real*img));
return c3;
}
public Complex divide(Complex c2)
{
Complex c3=new Complex();
c3.real=((real*c2.real) - (img*c2.img))/((c2.real * c2.real) - (c2.img* c2.img));
c3.img=((real*c2.img)+(c2.real*img))/((c2.real*c2.real) - (c2.img*c2.img));
return c3;
}
public void display(){
System.out.println("Reasult: " + real +"+i"+img);
}
}
*this is the seconed class with the main import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println ("Choose an operation :\n");
System.out.println (" 1. Add \n");
System.out.println (" 2. Subtract \n");
System.out.println (" 3. Multiply \n");
System.out.println (" 4. Divide \n");
System.out.println (" 5. Exit \n");
Scanner input = new Scanner (System.in);
Complex complexNum = new Complex ();
int choose = input.nextInt();
System.out.println(" Enter first number (real and imaginary parts): \n ");
double R1= input.nextInt();
double I1= input.nextInt();
complexNum.setReal(R1);
complexNum.setImg(I1);
System.out.println(" Enter second number (real and imaginary parts): \n");
double R2= input.nextInt();
double I2= input.nextInt();
complexNum.setReal(R2);
complexNum.setImg(I2);
if(choose == 1)
complexNum.add(complexNum);
if (choose ==2)
complexNum.subtract(complexNum);
if (choose == 3)
complexNum.multiply(complexNum);
if (choose ==4)
complexNum.divide(complexNum);
if(choose == 5)
//complexNum.Exit(0);
complexNum.display();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.