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 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.

##

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:

That's all about

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.
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 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 given String in Java? (solution)
- How to check if given String is palindrome or not 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 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 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)

**References**

## No comments:

## Post a Comment