-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNum.java
More file actions
50 lines (37 loc) · 1.3 KB
/
Copy pathComplexNum.java
File metadata and controls
50 lines (37 loc) · 1.3 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
import java.util.Scanner;
public class ComplexNum {
double real; //real part
double img; //image part
public ComplexNum(double real, double img) {
this.real = real;
this.img = img;
}
public ComplexNum CreateComplexNum(double real, double img){
return new ComplexNum(real, img);
}
//representation of a number like a+bi
@Override
public String toString() {
if (img == 0) return real + "";
if (real == 0) return img + "i";
if (img > 0) return real + "+" + img + "i";
return real + "" + img + "i";
}
//addition of two numbers
public ComplexNum sum(ComplexNum num2){
return new ComplexNum(this.real + num2.real, this.img + num2.img);
}
//multiplication of two numbers
public ComplexNum mult(ComplexNum num2){
return new ComplexNum((this.real*num2.real - this.img*num2.img),
(this.real*num2.img + num2.real*this.img));
}
//trigonometric form
public String trig(){
double module = Math.sqrt(Math.pow(real, 2) + Math.pow(img, 2));
double angle = Math.atan(img / real);
return module + "(cos " + angle + " + " + "isin " + angle + ")";
}
public static void main(String[] args){
}
}