-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.java
More file actions
50 lines (39 loc) · 1.2 KB
/
oops.java
File metadata and controls
50 lines (39 loc) · 1.2 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
class Complex {
int real;
int imaginary;
// Constructor
public Complex(int r, int i) {
this.real = r;
this.imaginary = i;
}
// Addition
public static Complex add(Complex a, Complex b) {
return new Complex((a.real + b.real), (a.imaginary + b.imaginary));
}
// Subtraction
public static Complex diff(Complex a, Complex b) {
return new Complex((a.real - b.real), (a.imaginary - b.imaginary));
}
// Printing Complex Number
public void printComplex() {
if (real == 0 && imaginary != 0) {
System.out.println(imaginary + "i");
} else if (real != 0 && imaginary == 0) {
System.out.println(real);
} else if (real == 0 && imaginary == 0) {
System.out.println("0");
} else {
System.out.println(real + (imaginary > 0 ? "+" : "") + imaginary + "i");
}
}
}
class Solution {
public static void main(String[] args) {
Complex c = new Complex(1, 2);
Complex d = new Complex(3, 4);
Complex added = Complex.add(c, d);
Complex differ = Complex.diff(c, d);
added.printComplex();
differ.printComplex();
}
}