-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotor.java
More file actions
46 lines (37 loc) · 912 Bytes
/
Motor.java
File metadata and controls
46 lines (37 loc) · 912 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
40
41
42
43
44
45
46
public class Motor {
double speed;
int id;
public Motor(int id) {
this.id = id;
}
public double get() {
return speed;
}
public void set(double speed) {
this.speed = speed;
if (speed > 1) {
set(1);
}
if (speed < -1) {
set(-1);
}
}
public void stop() {
set(0.0);
}
public String status() {
return "Motor #" + id + "=" + speed;
}
public static void main(String... args) {
Motor frontLeft = new Motor(1);
frontLeft.set(2);
System.out.println(frontLeft.status());
frontLeft.stop();
System.out.println(frontLeft.status());
Motor backRight = new Motor(2);
backRight.set(-0.5);
System.out.println(backRight.status());
backRight.stop();
System.out.println(backRight.status());
}
}