Addition/Subtraction of Complex Number: Java

Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java. It also explains about Do Nothing Constructor. Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part. In this program we are using parameterized constructor to copy the real and imaginary values … Continue reading “Addition/Subtraction of Complex Number: Java”

Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java.
It also explains about Do Nothing Constructor.

Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part.

In this program we are using parameterized constructor to copy the real and imaginary values of the objects created, into the local variables of the class calc.(float real, img;)

calc(float r, float i)
{
real = r;
img = i;
}

Passing values to parameterized constructor:

calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);

But since we are also creating a third object c3, which doesn’t pass any parameter values, we need to write a default constructor even though it doesn’t contain any definition in it. Such a constructor is called Do Nothing Constructor.

calc c3 = new calc(); //Not Passing any value, 
                      //hence it starts searching for default constructor.

This is Do Nothing Constructor:

calc() { }

Now the actual addition of complex number:
The return type is calc, as it has to return value to be stored in object c3(which is of type calc).
Here, we add the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.

calc add(calc c2)
{
calc res = new calc();
 
res.real = real + c2.real;
res.img = img + c2.img;
 
return(res);
}

Since c1 object called the add method, we need not again write c1.real and c1.img Instead we can directly write real and img.

c3 = c1.add(c2);

Similarly, subtraction of complex numbers:
Here, we subtract the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.

calc sub(calc c2)
{
calc res = new calc();
 
res.real = real - c2.real;
res.img  = img - c2.img;
 
return(res);
}

Call to Subtraction method:

c3 = c1.sub(c2);

Video Tutorial: Addition and Subtraction of Complex Number: Java


[youtube https://www.youtube.com/watch?v=tfiTMjwDqKM]

YouTube Link: https://www.youtube.com/watch?v=tfiTMjwDqKM [Watch the Video In Full Screen.]



Full Source Code:(Complex.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.lang.*;
 
class calc
{
float real, img;
 
calc() {}        // Do Nothing Constructor
 
calc(float r, float i)
{
real = r;
img = i;
}
 
void display()
{
System.out.println(real+" + i "+img);
}
 
calc add(calc c2)
{
calc res = new calc();
 
res.real = real + c2.real;
res.img = img + c2.img;
 
return(res);
}
 
calc sub(calc c2)
{
calc res = new calc();
 
res.real = real - c2.real;
res.img = img - c2.img;
 
return(res);
}
 
}
 
class Complex
{
public static void main(String args[])
{
calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);
 
System.out.println("C1 is: ");
c1.display();
System.out.println("C2 is: ");
c2.display();
 
calc c3 = new calc();
 
System.out.println("Addition of C1 and C2 is: ");
c3 = c1.add(c2);
c3.display();
 
System.out.println("Subtraction of C1 and C2 is: ");
c3 = c1.sub(c2);
c3.display();
}
}

Output:
C1 is:
12.5 + i 2.5

C2 is:
9.5 + i 0.5

Addition of C1 and C2 is:
22.0 + i 3.0

Subtraction of C1 and C2 is:
3.0 + i 2.0