Algorithm


Define ComplexNumber Class:

      • Create a class named ComplexNumber to represent complex numbers.
      • Include real and imaginary parts as instance variables.
      • Provide a constructor to initialize the complex number.
class ComplexNumber {
    double real;
    double imaginary;

    // Constructor to initialize the complex number
    public ComplexNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }
}

  1.  
  2. Create a Function for Addition:

    • Create a function, e.g., addComplexNumbers, that takes two ComplexNumber objects as parameters and returns a new ComplexNumber object representing their sum.
    • Calculate the sum of the real and imaginary parts separately.
    class ComplexOperations {
        // Function to add two complex numbers
        public static ComplexNumber addComplexNumbers(ComplexNumber num1, ComplexNumber num2) {
            double realSum = num1.real + num2.real;
            double imaginarySum = num1.imaginary + num2.imaginary;
    
            // Create a new complex number representing the sum
            ComplexNumber sum = new ComplexNumber(realSum, imaginarySum);
            return sum;
        }
    }
  3.  
  4. Test the Program:

    • In your main program, create instances of ComplexNumber representing the two complex numbers you want to add.
    • Call the addComplexNumbers function with these instances and print the result.
    public class ComplexNumberAddition {
        public static void main(String[] args) {
            // Create complex numbers
            ComplexNumber num1 = new ComplexNumber(3.5, 2.0);
            ComplexNumber num2 = new ComplexNumber(1.5, 4.5);
    
            // Add complex numbers
            ComplexNumber sum = ComplexOperations.addComplexNumbers(num1, num2);
    
            // Print the result
            System.out.println("Sum: " + sum.real + " + " + sum.imaginary + "i");
        }
    }
  5.  
  6. Run the Program:

    • Compile and run the program to see the result of adding the two complex numbers.

 

Code Examples

#1 Code Example- Add Two Complex Numbers

Code - Java Programming

public 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 n1 = new Complex(2.3, 4.5),
                n2 = new Complex(3.4, 5.0),
                temp;

        temp = add(n1, n2);

        System.out.printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
    }

    public static Complex add(Complex n1, Complex n2)
    {
        Complex temp = new Complex(0.0, 0.0);

        temp.real = n1.real + n2.real;
        temp.imag = n1.imag + n2.imag;

        return(temp);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Sum = 5.7 + 9.5i
Advertisements

Demonstration


Java Programing Example to Add Two Complex Numbers by Passing Class to a Function-DevsEnv