-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex_Number.txt
More file actions
107 lines (91 loc) · 2.87 KB
/
Copy pathComplex_Number.txt
File metadata and controls
107 lines (91 loc) · 2.87 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Complex Number Problem
Send Feedback
A ComplexNumber class contains two data members : one is the real part (R) and the other is imaginary (I) (both integers).
Implement the Complex numbers class that contains following functions -
1. constructor
You need to create the appropriate constructor.
2. plus -
This function adds two given complex numbers and updates the first complex number.
e.g.
if C1 = 4 + i5 and C2 = 3 +i1
C1.plus(C2) results in:
C1 = 7 + i6 and C2 = 3 + i1
3. multiply -
This function multiplies two given complex numbers and updates the first complex number.
e.g.
if C1 = 4 + i5 and C2 = 1 + i2
C1.multiply(C2) results in:
C1 = -6 + i13 and C2 = 1 + i2
4. print -
This function prints the given complex number in the following format :
a + ib
Note : There is space before and after '+' (plus sign) and no space between 'i' (iota symbol) and b.
Input Format :
Line 1 : Two integers - real and imaginary part of 1st complex number
Line 2 : Two integers - real and imaginary part of 2nd complex number
Line 3 : An integer representing choice (1 or 2) (1 represents plus function will be called and 2 represents multiply function will be called)
Output format :
Check details of 'print' function given above.
Sample Input 1 :
4 5
6 7
1
Sample Output 1 :
10 + i12
Sample Input 2 :
4 5
6 7
2
Sample Output 2 :
-11 + i58
/******************
* Following is the main function we are using internally.
* Refer this for completing the ComplexNumbers class
*
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int real1 = s.nextInt();
int imaginary1 = s.nextInt();
int real2 = s.nextInt();
int imaginary2 = s.nextInt();
ComplexNumbers c1 = new ComplexNumbers(real1, imaginary1);
ComplexNumbers c2 = new ComplexNumbers(real2, imaginary2);
int choice = s.nextInt();
if(choice == 1) {
// Add
c1.plus(c2);
c1.print();
}
else if(choice == 2) {
// Multiply
c1.multiply(c2);
c1.print();
}
else {
return;
}
}
******************/
public class ComplexNumbers {
// Complete this class
private int real;
private int imag;
ComplexNumbers(){
}
ComplexNumbers(int real, int imag){
this.real = real;
this.imag = imag;
}
public void plus(ComplexNumbers c2){
this.real = this.real + c2.real;
this.imag = this.imag + c2.imag;
}
public void multiply(ComplexNumbers c2){
int x = this.real; // Here x is used coz when real is updated, that updated value will be used in calc imag value after multiplication.
this.real = (this.real * c2.real) - (this.imag * c2.imag);
this.imag = (x * c2.imag) + (this.imag * c2.real);
}
public void print(){
System.out.print(this.real + " + i" + this.imag);
}
}