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

Updated on November 27, 2024
Add two complex numbers by passing class to a function header image

Introduction

Complex numbers are a fundamental concept in mathematics and programming that have applications in various fields such as engineering, physics, and even computer science. In programming, handling complex numbers involves understanding classes and methods specific to object-oriented programming. Java provides a robust platform to manage such types of data through class and object manipulations.

In this article, you will learn how to create a simple Java program to add two complex numbers. By passing instances of a class to a function, you can encapsulate behaviors related to complex numbers, thus enhancing code modularity and clarity.

Building the Complex Number Class

Define the Complex Number Class

  1. Start by defining a Java class named Complex.

  2. Include two private instance variables to store the real and imaginary parts of the complex numbers.

  3. Add a constructor to initialize these parts.

  4. Implement a method add(Complex b) within the class to handle the addition of complex numbers.

    java
    public class Complex {
        private double real;
        private double imag;
    
        public Complex(double real, double imag) {
            this.real = real;
            this.imag = imag;
        }
    
        public Complex add(Complex b) {
            return new Complex(this.real + b.real, this.imag + b.imag);
        }
    }
    

    This class defines a complex number with methods to construct and add complex numbers. When add() is called, it returns a new Complex instance representing the sum.

Utilizing the add Method

  1. Create two Complex instances representing the complex numbers you wish to add.

  2. Use the add method to compute the sum of these numbers.

  3. Show the result.

    java
    public class Main {
        public static void main(String[] args) {
            Complex c1 = new Complex(3, 2);
            Complex c2 = new Complex(4, -1);
            Complex sum = c1.add(c2);
            System.out.printf("Sum of the complex numbers: %.1f + %.1fi", sum.real, sum.imag);
        }
    }
    

    Here, two complex numbers, 3 + 2i and 4 - 1i, are added together. The resulting object, sum, holds the computed sum, which is printed to the console.

Printing Complex Numbers

Enhancing Readability With toString Method

  1. Implement the toString method in the Complex class to format the complex number as a string.

  2. Modify the Main class to use the toString method for displaying the complex number in a clear format.

    java
    public class Complex {
        private double real;
        private double imag;
    
        public Complex(double real, double imag) {
            this.real = real;
            this.imag = imag;
        }
    
        public Complex add(Complex b) {
            return new Complex(this.real + b.real, this.imag + b.imag);
        }
    
        @Override
        public String toString() {
            if (imag < 0)
                return real + " - " + (-imag) + "i";
            else
                return real + " + " + imag + "i";
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Complex c1 = new Complex(3, 2);
            Complex c2 = new Complex(4, -1);
            Complex sum = c1.add(c2);
            System.out.println("Sum of the complex numbers: " + sum);
        }
    }
    

    Adding the toString method improves how complex numbers are displayed, making results like 7.0 + 1.0i more readable to users.

Conclusion

Master the basics of handling complex numbers in Java by defining a class and methods that encapsulate specific behaviors, such as addition. The program example provided uses object-oriented principles to add two complex numbers by passing class instances to a function. This approach ensures that the operations related to complex number arithmetic are neatly packaged in modular, reusable components. Experiment further by adding methods for other arithmetic operations like subtraction, multiplication, and division to enhance the Complex class's functionality.