Program to add and Subtract Complex Numbers using Class in Java


Complex numbers are expressed as the sum of a real number and an imaginary number. Its general form is ‘a + ib’ where a and b represent real numbers and ‘i’ an imaginary number. For example, 5.4 + 2.6i here, 5.4 is the real part and 2.6i is the imaginary part.

The various application of complex number includes quantum mechanics, signal processing and so forth. In this article, we will create a Java program to add and subtract complex numbers using class. Although it supports almost all the basic mathematical operations.

Program to Add and Subtract Complex Numbers using Class

Before jumping into the program, let’s briefly discuss a few concepts useful for this problem −

User Defined Methods

Methods are blocks of code that can be reused multiple times to perform a single operation. It saves our time and also reduces the size of code.

Syntax

accessSpecifier nonAccessModifier return_Type method_Name(Parameters) {
   // Body of the method
}

Here,

accessSpecifier − It is used to set the accessibility of the method. It may be public, protected, default and private.

nonAccessModifier − It shows additional functionalities or behavior of the method such as static and final.

return_Type − The datatype a method is going to return. We use void keyword when method does not return anything.

method_Name − Name of the method.

parameters − It contains name of the variable followed by datatype.

Classes and Objects

Class is a blueprint of objects that contains the data members and methods. It is created using the keyword ‘class’. It defines the behavior of objects. Object is an instance of a class. When a class is created it does not occupy any memory. Only the object of that class occupies memory. We can create multiple objects of a single class if needed.

Syntax for class

accessSpecifier class NameOfClass {
   // code
}

Syntax for object

NameOfClass nameOfObject = new NameOfClass();

Working of Code

  • First, create a class named ‘Comp’ and its parameterized constructor. Inside this class, create two methods one for addition and another for subtraction.

  • The method ‘cmpAdd()’ will perform the addition operation. Define an object ‘sum’ and using this, we will add the real part of complex number first and then, the imaginary part.

  • The method ‘cmpSub()’ will perform the subtraction operation. Define an object ‘diff’ and using this, we will subtract the real part of complex number first and then, the imaginary part.

  • In the main() method, define two objects with the same name as the parameters of above methods. It will store the value of complex numbers.

  • Now, call the static methods using the class name and print the result.

Example

import java.util.*;
public class Comp {
   double frst, lst;
   Comp(double frst, double lst) {  
   // Constructor
     this.frst = frst;
     this.lst = lst;
   }
   public static Comp cmpAdd(Comp data1, Comp data2) {  
     // method to add
     Comp sum = new Comp(0.0, 0.0); 
     // object for addition
     sum.frst = data1.frst + data2.frst; 
     sum.lst = data1.lst + data2.lst; 
     return(sum);
   }
   public static Comp cmpSub(Comp data1, Comp data2) { 
     // method to subtract
     Comp diff = new Comp(0.0, 0.0); 
     // object for subtraction
     diff.frst = data1.frst - data2.frst; 
     diff.lst = data1.lst - data2.lst; 
     return(diff);
   }
   void displayComp() {  
     // method to display complex number
     System.out.println( frst + " + " + lst + "i ");
   }
   public static void main(String[] args) {
      // store value to the objects
      Comp data1 = new Comp(5.4, 2.6); 
      Comp data2 = new Comp(3, 1);
      System.out.println("The first complex number: ");
      data1.displayComp();
      System.out.println("The second complex number: ");
      data2.displayComp();
      //  to add and subtract
      Comp sum = cmpAdd(data1, data2);
      Comp diff = cmpSub(data1, data2);
      System.out.println("Sum is: " + sum.frst + " + " + sum.lst + "i ");
      System.out.print("Difference is: " + diff.frst + " + " + diff.lst + "i ");
   }
}

Output

The first complex number: 
5.4 + 2.6i 
The second complex number: 
3.0 + 1.0i 
Sum is: 8.4 + 3.6i 
Difference is: 2.4000000000000004 + 1.6i

Conclusion

In this article, we have discussed a Java program to perform addition and subtraction on complex numbers. During this discussion, we familiarize you with user defined methods, classes and objects.

Updated on: 16-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements