public class ComplexNumber { // variables to hold real and imaginary number of a complex number int realNum, imgNum; // constructor to initialize the complex number ComplexNumber(int r, int i) { this.realNum = r; this.imgNum = i; } public static ComplexNumber add(ComplexNumber cn1, ComplexNumber cn2) { // temporary complex number to store the sum of two numbers ComplexNumber temp = new ComplexNumber(0, 0); temp.realNum = cn1.realNum + cn2.realNum; temp.imgNum = cn1.imgNum + cn2.imgNum; // after addition returning result return temp; } public static void main(String args[]) { ComplexNumber cnObj1 = new ComplexNumber(7, 6); ComplexNumber cnObj2 = new ComplexNumber(2, 1); // calling add() to perform addition ComplexNumber tempObj = add(cnObj1, cnObj2); System.out.printf("Sum is: " + tempObj.realNum + " + " + tempObj.imgNum + "i"); } }