-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.java
More file actions
39 lines (33 loc) · 718 Bytes
/
Copy pathVehicle.java
File metadata and controls
39 lines (33 loc) · 718 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
31
32
33
34
35
36
37
38
39
class Vehicle { //represents a vehicle that moves between 20 and -20, to illustrate OOP
public int location;
public Vehicle() {
location = 0;
}
public Vehicle(int loc){
if(loc <= 20 && loc >= -20){
location = loc;
} else {
location = 0;
}
}
public void forward() {
if(location < 20) {
location++;
}
}
public void backward() {
if(location > -20) {
location--;
}
}
public int getLocation() {
return location;
}
public String toString() {
StringBuilder string = new StringBuilder();
for(int i = -20; i < location; i++) {
string.append(" ");
}
string.append("@");
return string.toString();
}