How to calculate sum and difference of two complex numbers in Java? Example

From the last couple of articles, I am writing about coding exercises for beginners e.g. yesterday you learned how to write a program from matrix multiplication in Java (see here) and a couple of days back, you have learned the recursive binary search algorithm. To continue that tradition today I am going to show you how to write a program for calculating the sum and difference of two complex numbers in Java. If you remember the complex number from you maths classes, it has two-part real and imaginary and to add a complex number we add their real and imaginary part separately, similar to subtract complex number we minus their real and imaginary part separately. 

For example, if the first complex number is A + iB and the second complex number is X + iY then the addition of these two complex numbers will be equal to (A +X ) + i(B + Y).

Similarly, subtraction of these two complex numbers would be equal to (A - X) + i(B -Y). So, the formula is quite simple but the key is to implement that in a program using object-oriented techniques.

In order to solve the problem in an object-oriented way, I have created a ComplexNumber class to encapsulate both real and imaginary parts. This class has the getter to retrieve both real and imaginary parts but I have not provided any setter method because I made this class Immutable. 

This means, you cannot change the value of a complex number once created, any modification will result in the creation of a new ComplexNumber, much like String because String class is also immutable in Java.

This class also has methods to calculate the sum and difference of two complex numbers. The sum() method accepts a complex number and returns the sum of this complex number with the given complex number. Since ComplexNumber is an Immutable class, it returns a new instance of ComplexNumber and doesn't modify the existing instance.

Similarly, the difference() method also accepts a ComplexNumber and calculates the difference between this instance and given a complex number. It also returns a new ComplexNumber instance, whose real and imaginary parts are equal to the difference of real parts of two complex numbers.

If you are interested in learning more about immutability and its benefit, I suggest you join a good Java online course to learn fundamental Java concepts,  a perfect companion in your Java journey.




Java Program to calculate sum and difference of two Complex Numbers

Here is our complete Java program to calculate the sum and difference of two complex numbers in Java. It's a simple program because addition is nothing but adding the real and imaginary parts of both complex numbers and subtraction is also subtracting both the real and imaginary parts of the complex number.  

Here is a nice diagram to explain the addition of complex numbers, may this will remind you of the maths lesson from school days:

How to perform addition and subtraction of complex numbers in Java



Java program of addition and subtraction of two Complex Numbers

import java.util.Scanner;

/*
 * Java Program to add and subtract two complex numbers
 * This program will calculate sum and difference of two
 * given a complex number in Java.
 */
public class Main {

  public static void main(String[] args) {

    // first complex number
    ComplexNumber c1 = new ComplexNumber(2, 4);
    ComplexNumber c2 = new ComplexNumber(3, 5);

    ComplexNumber sum = c1.sum(c2);
    ComplexNumber difference = c1.difference(c2);

    System.out.println("first complex number: " + c1);
    System.out.println("second complex number: " + c2);
    System.out.println("sum of two complex numbers: " + sum);
    System.out.println("difference of two complex numbers: " + difference);

  }
}

/*
 * A class to represent a complex number. A complex number has two parts, real
 * and imaginary. Make this class Immutable as it's a value class.
 */
class ComplexNumber {
  private final double real;
  private final double imaginary;

  public ComplexNumber(double real, double imaginary) {
    this.real = real;
    this.imaginary = imaginary;
  }

  public ComplexNumber sum(ComplexNumber other) {
    double r = this.real + other.real;
    double i = this.imaginary + other.imaginary;
    return new ComplexNumber(r, i);
  }

  public ComplexNumber difference(ComplexNumber other) {
    double r = this.real - other.real;
    double i = this.imaginary - other.imaginary;
    return new ComplexNumber(r, i);
  }

  public double getReal() {
    return real;
  }

  public double getImaginary() {
    return imaginary;
  }

  @Override
  public String toString() {
    return real + " + " + imaginary + "i";
  }

}

Output
first complex number: 2.0 + 4.0i
second complex number: 3.0 + 5.0i
sum of two complex numbers: 5.0 + 9.0i
difference of two complex numbers: -1.0 + -1.0i


That's all about how to calculate the sum and difference of two complex numbers in Java. In this program, we have followed object-oriented programming concepts to represent a complex number and methods for the addition and subtraction of complex numbers. You can also create some JUnit test to test the add() and subtract() or sum() and difference() methods. 


Other coding exercises for programmers
  • How to implement binary search using recursion in Java? (solution)
  • How to do matrix multiplication in Java? (example)
  • How to calculate the average of all numbers of an array in Java? (program)
  • How to remove duplicate characters from String in Java? (solution)
  • How to reverse an array in place in Java? (solution)
  • How to find if given Integer is Palindrome in Java? (solution)
  • How to print Fibonacci series in Java (solution)
  • How to check if a year is a leap year in Java? (solution)
  • How to reverse a String in place in Java? (solution)
  • How to check if the given number is prime in Java (solution)
  • How to find the highest occurring word from a given file in Java? (solution)
  • How to implement Linear Search in Java? (solution)
  • How to calculate the square root of a given number in Java? (solution)
  • How to calculate the Area of Triangle in Java? (program)
  • How to find all permutations of a given String in Java? (solution)
  • How to remove duplicate elements from the array in Java? (solution)
  • How to check if two given Strings are Anagram in Java? (solution)
  • How to check if a String contains duplicate characters in Java? (solution)
  • How to reverse words in a given String in Java? (solution)
  • How to calculate the sum of all elements of an array in Java? (program)
  • How to check if two rectangles intersect with each other in Java? (solution)
  • How to count vowels and consonants in a given String in Java? (solution)
  • How to check if the given String is palindrome or not in Java? (solution)

P. S. - It's a good exercise to start with. If you are interested in more programming and coding challenges then you can also solve problems given in Exercises for Programmers: 57 Challenges to Develop Your Coding Skills book. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.