Complex in IAVA.(Create Complex class, constructor, method & to add, subtract and multiply two complex numbers and to return the real and imaginary parts)
CODING:
class Complex
{
double real;
double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public static void main(String[] args) {
Complex o1 = new Complex(5.3, 3.5);
Complex o2 = new Complex(6.4, 2.0),o3 ;
o3 = add(o1, o2);
System.out.printf("Addition = %.1f + %.1fi\n", o3.real, o3.imag);
o3 = sub(o1, o2);
System.out.printf("Subtract = %.1f + %.1fi\n", o3.real, o3.imag);
o3 = mul(o1, o2);
System.out.printf("Multiply= %.1f + %.1fi", o3.real, o3.imag);
}
public static Complex add(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real + o2.real;
o3.imag = o1.imag + o2.imag;
return(o3);
}
public static Complex sub(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real - o2.real;
o3.imag = o1.imag - o2.imag;
return(o3);
}
public static Complex mul(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real * o2.real;
o3.imag = o1.imag * o2.imag;
return(o3);
}
}
{
double real;
double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public static void main(String[] args) {
Complex o1 = new Complex(5.3, 3.5);
Complex o2 = new Complex(6.4, 2.0),o3 ;
o3 = add(o1, o2);
System.out.printf("Addition = %.1f + %.1fi\n", o3.real, o3.imag);
o3 = sub(o1, o2);
System.out.printf("Subtract = %.1f + %.1fi\n", o3.real, o3.imag);
o3 = mul(o1, o2);
System.out.printf("Multiply= %.1f + %.1fi", o3.real, o3.imag);
}
public static Complex add(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real + o2.real;
o3.imag = o1.imag + o2.imag;
return(o3);
}
public static Complex sub(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real - o2.real;
o3.imag = o1.imag - o2.imag;
return(o3);
}
public static Complex mul(Complex o1, Complex o2) {
Complex o3 = new Complex(0.0, 0.0);
o3.real = o1.real * o2.real;
o3.imag = o1.imag * o2.imag;
return(o3);
}
}
Comments
Post a Comment