-
Notifications
You must be signed in to change notification settings - Fork 117
/
ComplexNumbers
59 lines (47 loc) · 1.24 KB
/
ComplexNumbers
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
public class ComplexNumbers {
private int real;
private 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 multiply(ComplexNumbers c2)
{
int temp=this.real;
this.real = (this.real*c2.real)-(this.imaginary*c2.imaginary);
this.imaginary =temp*c2.imaginary+this.imaginary*c2.real;
}
public void print()
{
System.out.println(this.real+" + "+"i"+this.imaginary );
}
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;
}
}
}