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 and a couple of days back, you have learned

Similarly, subtraction of these two complex number would be equal to

In order to solve the problem in an object-oriented way, I have created a ComplexNumber class to encapsulate both real and imaginary part. This class has the getter to retrieve both real and imaginary part but I have not provided any setter method because I made this class Immutable. This means, you cannot change the value of 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 sum and difference of two complex number. The sum() method accepts a complex number and return the sum of this complex number with the given complex number. Since ComplexNumber is Immutable class, it return a new instance of ComplexNumber and doesn't modify the existing instance.

Similarly, the difference() method also accept a ComplexNumber and calculate the difference between this instnace 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 number.

If you are interested in learning more about immutability and its benefit, I suggest you read a good core Java book like Java: A Beginner's Guide by Herbert Schildt, a perfect companion in your Java journey.

####

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";

}

}

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 sum and difference of two complex numbers in Java. In this program, we have followed object oriented programming concept to represent a complex number and methods for addition and subtraction of complex numbers. You can also create some JUnit test to test the add() and subtract() or sum() and difference() methods. It's a good exercise to start with. If you 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.

**recursive binary search algorithm**. To continue that tradition today I am going to show you how to write a program for calculating 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 first complex number is**A + iB**and the second complex number is**X + iY**then the addition of these two complex number will be equal to**(A +X ) + i(B + Y).**Similarly, subtraction of these two complex number 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 technique.In order to solve the problem in an object-oriented way, I have created a ComplexNumber class to encapsulate both real and imaginary part. This class has the getter to retrieve both real and imaginary part but I have not provided any setter method because I made this class Immutable. This means, you cannot change the value of 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 sum and difference of two complex number. The sum() method accepts a complex number and return the sum of this complex number with the given complex number. Since ComplexNumber is Immutable class, it return a new instance of ComplexNumber and doesn't modify the existing instance.

Similarly, the difference() method also accept a ComplexNumber and calculate the difference between this instnace 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 number.

If you are interested in learning more about immutability and its benefit, I suggest you read a good core Java book like Java: A Beginner's Guide by Herbert Schildt, 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 sum and difference of two complex numbers in Java. It's a simple program because addition is nothing but adding real and imaginary part of both complex number and subtraction is also subtracting both real and imaginary part of the complex number. Here is a nice diagram to explain addition of complex number, may this will remind you the maths lesson from school days:**

####
**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 sum and difference of two complex numbers in Java. In this program, we have followed object oriented programming concept to represent a complex number and methods for addition and subtraction of complex numbers. You can also create some JUnit test to test the add() and subtract() or sum() and difference() methods. It's a good exercise to start with. If you 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.