-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_complex_number_class.java
58 lines (49 loc) · 1.25 KB
/
C_complex_number_class.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
/******************
* 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 {
int real;
int imaginary;
public ComplexNumbers(int real, int imaginary){
this.real = real;
this.imaginary = imaginary;
}
public void plus(ComplexNumbers C2){
this.real = this.real + C2.real;
this.imaginary = this.imaginary + C2.imaginary;
}
public void print(){
System.out.println(real + " + "+"i"+imaginary);
}
public void multiply(ComplexNumbers C2){
int real = this.real*C2.real - this.imaginary*C2.imaginary;
int imaginary = this.real*C2.imaginary + this.imaginary*C2.real;
this.real = real;
this.imaginary = imaginary;
}
}