Untitled
import java.util.Scanner; class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public Complex add(Complex other) { return new Complex(this.real + other.real, this.imag + other.imag); } public Complex subtract(Complex other) { return new Complex(this.real - other.real, this.imag - other.imag); } public Complex multiply(Complex other) { double realPart = this.real * other.real - this.imag * other.imag; double imagPart = this.real * other.imag + this.imag * other.real; return new Complex(realPart, imagPart); } public void display() { System.out.println(this.real + " + " + this.imag + "i"); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Taking first complex number input System.out.print("Enter real part of first complex number: "); double real1 = scanner.nextDouble(); System.out.print("Enter imaginary part of first complex number: "); double imag1 = scanner.nextDouble(); // Taking second complex number input System.out.print("Enter real part of second complex number: "); double real2 = scanner.nextDouble(); System.out.print("Enter imaginary part of second complex number: "); double imag2 = scanner.nextDouble(); // Creating Complex objects Complex c1 = new Complex(real1, imag1); Complex c2 = new Complex(real2, imag2); // Performing operations Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); // Displaying results System.out.print("Sum: "); sum.display(); System.out.print("Difference: "); difference.display(); System.out.print("Product: "); product.display(); scanner.close(); } }
Leave a Comment