-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.java
92 lines (59 loc) · 1.85 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// A complex number is a number that can be expressed in the form a+bi, where a and b are real numbers, and i is a solution of equation x2=-1.
// Write a program to Add and subtract two complex numbers by creating a class Complex number in which :
// The complex numbers will be initialized with the help of the constructor.
// The addition and subtraction will be performed with the help of function calling.
// Example :
// Input : a1= 4 b1=8 a2=5 b2=7
// Output : Sum = 9+ i15 Difference = -1+i
// Explanation:
// (4 + i8) + (5 + i7)
// = (4 + 5) + i(8 + 7)
// = 9 + i15 (4 + i8) - (5 + i7)
// = (4 - 5) + i(8 - 7)
// = -1 + i
import java.util.*;
class Complex {
int real, imaginary;
Complex()
{
}
Complex(int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
Complex subtractComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real - C2.real;
temp.imaginary = C1.imaginary - C2.imaginary;
return temp;
}
void printComplexNumber()
{
System.out.println("Complex number: "+ real + " + "+ imaginary + "i");
}
}
public class Main{
public static void main(String[] args)
{
Complex C1 = new Complex(4, 8);
C1.printComplexNumber();
Complex C2 = new Complex(5, 7);
C2.printComplexNumber();
Complex C3 = new Complex();
C3 = C3.addComp(C1, C2);
System.out.print("Sum of ");
C3.printComplexNumber();
C3 = C3.subtractComp(C1, C2);
System.out.print("Difference of ");
C3.printComplexNumber();
}
}