-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComplexNumbers.java
59 lines (49 loc) · 1.01 KB
/
ComplexNumbers.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
import java.util.Scanner;
public class ComplexNumbers
{
private int real,imag;
public ComplexNumbers(int real,int imag)
{
this.real=real;
this.imag=imag;
}
public void plus(ComplexNumbers c2)
{
this.real+=c2.real;
this.imag+=c2.imag;
}
public void multiply(ComplexNumbers c2)
{
int Real=this.real;
int Imag=this.imag;
this.real=Real*c2.real-Imag*c2.imag;
this.imag=Real*c2.imag+c2.real*Imag;
}
public void print()
{
System.out.println(real+" "+"+"+" "+"i"+imag);
}
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;
}
}
}