Bonjour ,
Je fais un exercice qui me pose pas mal de problème.
L'exercice est le suivant : je dois créer une classe RobotDirectionnel qui se dirige dans une certaines directions.
Les variables d'instances sont un robot et une direction
Et je dois implémenter des méthodes suivantes : void avancer , tournerAdroite , tournerAgauche, avec getX, getY , getDirection et ToString
Voici ce que j'ai fait pour ma classe
Pour celle de test
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import robots.jar; public class RobotDirectionnel { private RobotVisuel r; private int direction; public RobotDirectionnel(int x,int y,int haut,int bas,PlateauVisuel v) { r=new RobotVisuel(x,y,v); } public void avancer(){ r.moveRight(); } public void peutAvancer() { boolean avancer,adroite,agauche; avancer=r.canMoveRight(); agauche=r.canMoveUp(); adroite=r.canMoveDown(); } public void tournerADroite() { r.moveDown(); } public void tournerAGauche() { r.moveUp(); } public int getx() { return this.r.getX(); } public int gety() { return this.r.getY(); } public int getDirection() { return this.r.getX() + this.r.getY(); } public String toString() { return System.out.println(r); } }
Je ne comprends pas à quoi getDirection peu bien me servir et comment modifier les positions du robot en x et y et à quoi sert le haut et le bas ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import robots.jar; public class TestRobotDirectionnel { public static void main(String[] args) { int x=2,y=2,haut=3,bas=1; PlateauVisuel v=new PlateauVisuel("Robot Directionnel"); RobotDirectionnel r=new RobotDirectionnel(x,y,haut,bas,v); r.avancer(); r.avancer(); r.tournerADroite(); r.tournerADroite(); r.peutAvancer(); r.avancer(); r.avancer(); r.tournerAGauche(); r.tournerAGauche(); System.out.println(r); // Méthode toString exécutée } }
Merci
Partager