-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.java
86 lines (66 loc) · 1.9 KB
/
Complex.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
76
77
78
79
80
81
82
83
84
85
86
package prg.es2;
import prg.es3.Numero;
public class Complex extends Numero{
private double real;
private double imaginary;
//COSTRUTTORI
public Complex(){}
public Complex(double real){
this(real, 0);
}
public Complex(double real, double imaginary){
this.setReal(real).setImaginary(imaginary);
}
//Metodi SET
public Complex setReal(double real){
this.real = real;
return this;
}
public Complex setImaginary(double imaginary){
this.imaginary = imaginary;
return this;
}
//Metodi GET
public double getReal(){
return this.real;
}
public double getImaginary(){
return this.imaginary;
}
//Operazioni
@Override
public Numero somma(Numero add){
if(add instanceof Complex){
Complex complexAdd = (Complex) add;
return new Complex(this.getReal() + complexAdd.getReal(), this.getImaginary() + complexAdd.getImaginary());
} else {
throw new ArithmeticException("Operandi Eterogenei");
}
}
@Override
public Numero sottrai(Numero sott){
if(sott instanceof Complex){
Complex complexSott = (Complex) sott;
return new Complex(this.getReal() - complexSott.getReal(), this.getImaginary() - complexSott.getImaginary());
} else {
throw new ArithmeticException("Operandi Eterogenei");
}
}
public double distance(Complex other){
return Math.sqrt(Math.pow(this.getReal()-other.getReal(), 2)+Math.pow(this.getImaginary()-other.getImaginary(), 2));
}
//Metodo EQUALS
public boolean equals(Complex other){
if(Math.abs(this.getReal() - other.getReal()) < 1e-10 && Math.abs(this.getImaginary() - other.getImaginary()) < 1e-10){
return true;
}
return false;
}
//Metodo TOSTRING
public String toString(){
if(this.getImaginary()>= 0){
return this.getReal() + " +i" + this.getImaginary();
}
return this.getReal() + " -i" + Math.abs(this.getImaginary());
}
}