-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumberProblem.java
More file actions
52 lines (41 loc) · 1.37 KB
/
ComplexNumberProblem.java
File metadata and controls
52 lines (41 loc) · 1.37 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
import java.util.Scanner;
public class ComplexNumberProblem {
private int real;
private int imaginary;
public ComplexNumberProblem(int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public void plus(ComplexNumberProblem c2) {
this.real = (this.real + c2.real);
this.imaginary = (this.imaginary + c2.imaginary);
}
public void multiply(ComplexNumberProblem 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();
ComplexNumberProblem c1 = new ComplexNumberProblem(real1, imaginary1);
ComplexNumberProblem c2 = new ComplexNumberProblem(real2, imaginary2);
int choice = s.nextInt();
s.close();
if (choice == 1) {
c1.plus(c2);
c1.print();
} else if (choice == 2) {
c1.multiply(c2);
c1.print();
} else {
return;
}
}
}