Pr-7
class Complex {
double real, imaginary;
public Complex() {
real = 0;
imaginary = 0;
}
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public Complex(Complex other) {
this.real = other.real;
this.imaginary = other.imaginary;
}
public Complex add(Complex other) {
double realSum = this.real + other.real;
double imaginarySum = this.imaginary + other.imaginary;
return new Complex(realSum, imaginarySum);
}
public void display() {
if (imaginary >= 0)
System.out.println(real + " + " + imaginary + "i");
else
System.out.println(real + " - " + (-imaginary) + "i");
}
}
public class Main {
public static void main(String[] args) {
Complex c1 = new Complex();
Complex c2 = new Complex(4.5, 3.2);
System.out.print("Complex number c1: ");
c1.display(); pr-7
System.out.print("Complex number c2: ");
c2.display();
Complex c3 = c1.add(c2);
System.out.print("Sum of c1 and c2: ");
c3.display();
Complex c4 = new Complex(c3);
System.out.print("Copy of c3 (c4): ");
c4.display();
}
}