-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nave.java
125 lines (101 loc) · 2.73 KB
/
Nave.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package prg.testiEsami.es2;
public class Nave{
private String code;
private double x;
private double y;
private double speed;
private double direction;
private double length;
private double width;
private int numberOfPassenger;
//costruttori
public Nave(String code, double x, double y, double speed, double direction, double length, double width, int numberOfPassenger){
this.setCode(code).setX(x).setY(y).setSpeed(speed).setDirection(direction).setLength(length).setWidth(width).setPassenger(numberOfPassenger);
}
//metodi set
public Nave setCode(String code){
this.code = code;
return this;
}
public Nave setX(double x){
this.x = x;
return this;
}
public Nave setY(double y){
this.y = y;
return this;
}
public Nave setSpeed(double speed){
this.speed = speed;
return this;
}
public Nave setDirection(double direction){
this.direction = Math.abs(direction)%360;
return this;
}
public Nave setLength(double length){
this.length = Math.abs(length);
return this;
}
public Nave setWidth(double width){
this.width = Math.abs(width);
return this;
}
public Nave setPassenger(int numberOfPassenger){
this.numberOfPassenger = numberOfPassenger;
return this;
}
//metodi get
public String getCode(){
return this.code;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public double getSpeed(){
return this.speed;
}
public double getDirection(){
return this.direction;
}
public double getLength(){
return this.length;
}
public double getWidth(){
return this.width;
}
public int getPassenger(){
return this.numberOfPassenger;
}
//metodo toString
public String toString(){
return "CODE: " + this.getCode() +
"\tPOSITION: (" + this.getX() + ", " + this.getY() +
")\tSPEED: " + getSpeed() +
"\tDIRECTION: " + this.getDirection() +
"\tDIMENSION: " + this.getLength() + "x" + this.getWidth() +
"\tN.PASSENGERS: " + this.getPassenger();
}
//metodo avanza (vedi suggerimenti)
public Nave avanza(){
x+= this.getSpeed()*Math.cos(this.getDirection()*3.14/180);
y+= this.getSpeed()*Math.sin(this.getDirection()*3.14/180);
return this;
}
//metodo equals
public boolean equals(Object other){ //dato che il codice è univoco, è assurdo che esistano due navi con stesso codice => sono la stessa nave
if(other instanceof Nave){
Nave otherNave = (Nave) other;
if( this.getCode().equals(otherNave.getCode())){
return true;
}
}
return false;
}
public double distance(Nave other){
return Math.sqrt(Math.pow(this.getX()-other.getX(), 2) + Math.pow(this.getY()-other.getY(), 2));
}
}