Back to Java
2026-04-135 min read

Java Program to Add Two Complex Numbers by Passing Class to a Function

Learn Java Program to Add Two Complex Numbers by Passing Class to a Function step by step with clear examples and exercises.

Why This Matters

Welcome back! In this lesson, we'll dive into a practical Java program that adds two complex numbers using a function and passing the class as an argument. This will help you understand object-oriented programming concepts better and prepare for interviews or real-world coding challenges.

Why This Matters

In software development, dealing with complex mathematical operations like adding complex numbers is common. By understanding how to create a class for complex numbers and write functions to perform these operations, you'll be well-prepared for various programming tasks, including exams, interviews, or real bug fixes.

Prerequisites

To follow along with this lesson, you should have a good understanding of the following Java concepts:

  • Basic syntax and data types (e.g., int, double, String)
  • Variables and assignment
  • Methods and functions
  • Object-oriented programming (classes, objects)

Core Concept

To create a program that adds complex numbers, we'll start by defining a Complex class with two member variables: real for the real part of the number and imag for the imaginary part. We'll also include a constructor to initialize these values when creating new instances of the Complex class.

public class Complex {
double real;
double imag;

// Constructor to initialize the real and imaginary parts
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
}

Next, we'll create a static method add() that takes two Complex objects as arguments and returns the sum of their real and imaginary parts as another Complex object.

public class Complex {
// ... (previous code)

// Static method to add two complex numbers
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);
}
}

Now that we have our class and method, let's create two Complex objects (n1 and n2) with some values and add them using the add() method.

public class Main {
public static void main(String[] args) {
Complex n1 = new Complex(3.0, 4.0); // Real: 3, Imaginary: 4
Complex n2 = new Complex(2.0, 5.0); // Real: 2, Imaginary: 5
Complex sum = Complex.add(n1, n2);

System.out.printf("Sum of complex numbers %s and %s is: %s + %si", n1.real, n1.imag, sum.real, sum.imag);
}
}

When you run this program, it will output the following result:

Sum of complex numbers 3.0 and 4.0 is: 5.0 + 9.0

Worked Example

Let's walk through the code step by step to understand how it works:

  1. We define a Complex class with two member variables (real and imag) and a constructor to initialize them.
  2. In the main() method, we create two instances of the Complex class (n1 and n2) with specific values for the real and imaginary parts.
  3. We call the static add() method on the Complex class, passing our two complex numbers as arguments. The add() method initializes a new Complex object (temp), adds the real and imaginary parts of the input complex numbers, and returns it.
  4. Finally, we print the sum of the complex numbers using the printf() method from the System.out class.

Common Mistakes

  1. Forgetting to initialize the member variables in the constructor: Make sure you assign values to real and imag inside the constructor.
  2. Using the wrong data type for the real or imaginary parts: Ensure that both the real and imaginary parts are of type double.
  3. Incorrectly implementing the addition operation: Verify that the addition of the real and imaginary parts is performed correctly, taking into account the order of operations (i.e., performing multiplication before addition when necessary).
  4. Not returning the sum from the add() method: Make sure to return the new Complex object created in the add() method.
  5. Misunderstanding the concept of static methods: Remember that static methods belong to the class, not an instance of the class, and can be called without creating an object of the class.

Practice Questions

  1. Write a Java program to subtract two complex numbers using the Complex class and a static method named subtract().
  2. Modify the add() method in the Complex class to accept and return a third complex number representing the difference between the first two complex numbers.
  3. Create a new method called multiply() in the Complex class that multiplies two complex numbers and returns the result as another complex number.
  4. Write a Java program to find the modulus (magnitude) of a complex number using the Complex class.

FAQ

  1. Why did you use a static method for adding complex numbers? Using a static method allows us to add complex numbers without creating an instance of the Complex class, making the code more flexible and efficient.
  2. Can I create an immutable Complex class where the real and imaginary parts cannot be changed after creation? Yes, you can make the Complex class immutable by declaring the member variables as final and not providing setter methods for them.
  3. What if I want to perform complex arithmetic operations like multiplication or division? You can create additional static methods in the Complex class to handle these operations, just like we did with the add() method.
  4. How can I represent a complex number in polar form using the Complex class? To represent a complex number in polar form (magnitude and angle), you'll need to convert it from rectangular form. You can add methods to the Complex class to calculate the magnitude and angle, or create new classes for the polar representation if needed.
Java Program to Add Two Complex Numbers by Passing Class to a Function | Java | XQA Learn