-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathComplexNumber.java
75 lines (68 loc) · 3.04 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* A class that represents Complex Numbers
* @author David Nguyen
* @since 11/14/2022
*/
public class ComplexNumber implements ComplexNumberInterface{
// A field representing the real part
private ArbitraryFloatingPointNumbers real;
// A field representing the imaginary part
private ArbitraryFloatingPointNumbers imaginary;
/**
* Creates a Complex Number.
* Assigns the class's fields the values that the constructor got when a ComplexNumber object is created.
* @param real An ArbitraryFloatingPointNumbers that represents a real part
* @param imaginary An ArbitraryFloatingPointNumbers that represents an imaginary part
*/
public ComplexNumber (ArbitraryFloatingPointNumbers real, ArbitraryFloatingPointNumbers imaginary) {
this.real = real;
this.imaginary = imaginary;
}
/**
* A method that retrieves the real part of the complex number.
* @return A floating point number that represents the real part of a complex number
*/
public ArbitraryFloatingPointNumbers getRealPart() {
return real;
}
/**
* A method that retrieves the imaginary part of the complex number.
* @return ArbitraryFloatingPointNumbers A floating point number that represents the imaginary part of a complex number
*/
public ArbitraryFloatingPointNumbers getImaginaryPart() {
return imaginary;
}
/**
* A method that retrieves the imaginary part of the complex number.
* @param number Any ComplexNumber object that is given as input
* @return A Complex Number that is the result of the addition between this ComplexNumber and the input number
*/
public ComplexNumber add(ComplexNumberInterface number) {
ArbitraryFloatingPointNumbers newReal = this.getRealPart().add(this.getRealPart(), number.getRealPart());
ArbitraryFloatingPointNumbers newImaginary = this.getImaginaryPart().add(this.getImaginaryPart(), number.getImaginaryPart());
ComplexNumber result = new ComplexNumber (newReal, newImaginary);
return result;
}
/**
* A method that compares any two complex numbers.
* @param value1 Any ComplexNumber object that is given as input
* @param value2 Any ComplexNumber object that is given as input
* @return A boolean (true or false) depending on equality of these two numbers
*/
public boolean equals(ComplexNumber value1, ComplexNumber value2) {
return (value1.getRealPart().equals(value2.getRealPart())) && (value1.getImaginaryPart().equals(getImaginaryPart()));
}
/**
* A method that returns the string representation of this complex number.
* @return A String that is the proper string representation of the complex number
*/
public String toString() {
String output = new String();
if (!getImaginaryPart().isNegativeNumber()) {
output = (getRealPart() + " + " + getImaginaryPart() + "i");
}
else
output = (getRealPart() + " " + getImaginaryPart() + "i");
return output;
}
}