-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
42 lines (35 loc) · 1.19 KB
/
Copy pathComplex.java
File metadata and controls
42 lines (35 loc) · 1.19 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class Complex {
private final double re, im;
public Complex(double real, double imaginary) {
re = real;
im = imaginary;
}
public Complex plus(Complex b) {
double real = re + b.re;
double imaginary = im + b.im;
return new Complex(real, imaginary);
}
public Complex times(Complex b) {
double real = re * b.re - im * b.im;
double imaginary = re * b.im + im * b.re;
return new Complex(real, imaginary);
}
public double abs() {
return Math.sqrt(re * re + im * im);
}
public String toString() {
return re + " + " + im + "i";
}
public static void main(String[] args) {
Complex a = new Complex(3, -4);
Complex b = new Complex(12, 5);
System.out.println("a = " + a + "\n" + "b = " + b);
System.out.println("a+b = " + a.plus(b));
System.out.println("a*b = " + a.times(b));
}
}