forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
30 lines (24 loc) · 830 Bytes
/
Complex.java
File metadata and controls
30 lines (24 loc) · 830 Bytes
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
public class Complex {
// These are the data of the ADT
private double real;
private double imaginary;
// These are the operations of the ADT
public Complex(double real, double anImaginary) {
this.real = real;
imaginary = anImaginary;
}
public Complex plus(Complex other) {
double resultReal = this.real + other.real;
double resultImaginary = this.imaginary + other.imaginary;
return new Complex(resultReal, resultImaginary);
}
public String toString() {
return "Complex(" + real + ", " + imaginary + ")";
}
public static void main(String[] args) {
Complex a = new Complex(1.0, 2.0);
Complex b = new Complex(3.0, 4.0);
Complex c = a.plus(b);
System.out.println(a + " plus " + b + " = " + c);
}
}