-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNumber.java
54 lines (37 loc) · 1.11 KB
/
ComplexNumber.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
package ClassesAndObjects;
public class ComplexNumber {
private int real;
private int imaginary;
// Default Constructor
public ComplexNumber() {
}
public ComplexNumber(int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public void print() {
System.out.println(real + " + " + "i" + imaginary);
}
public void setReal(int real) {
this.real = real;
}
public void setimaginary(int imaginary) {
this.imaginary = imaginary;
}
public void add(ComplexNumber c2) {
this.real = this.real + c2.real;
this.imaginary = this.imaginary + c2.imaginary;
}
public void multiply(ComplexNumber c2) {
int newReal = (this.real * c2.real) - (this.imaginary * c2.imaginary);
int newImaginary = (this.real * c2.imaginary) + (this.imaginary * c2.real);
this.real = newReal;
this.imaginary = newImaginary;
}
public static ComplexNumber add(ComplexNumber c2, ComplexNumber c3) {
int newReal = c2.real + c3.real;
int newImg = c2.imaginary + c3.imaginary;
ComplexNumber c4 = new ComplexNumber(newReal, newImg);
return c4;
}
}