Académique Documents
Professionnel Documents
Culture Documents
Correction TP 5
Exercice 1
public class Robot {
private String nom, direction;
private int x, y;
public Robot(String nom, int x, int y, String direction){
this.nom = nom;
this.x = x;
this.y = y;
this.direction = direction;
}
public void avance(){
if (direction.equals("Est"))
x++;
else if (direction.equals("Ouest"))
x--;
else if (direction.equals("Nord"))
y++;
else
y--;
}
public void droite(){
if (direction.equals("Nord"))
direction = "Est";
else if (direction.equals("Est"))
direction = "Sud";
else if (direction.equals("Sud"))
direction ="Ouest";
else
direction = "Nord";
}
public void afficher(){
System.out.println("Nom : " + nom);
System.out.println("Position : ("+ x + "," + y +"));
System.out.println(Direction : " +direction);
}
public void gauche(){};
public void demiTour(){};
public void avance(int pas){};
}
1
public void avance(int nbrePas){
for (int i = 0 ; i < nbrePas ; i++)
super.avance();
}
public void gauche(){
for (int i = 0 ; i < 3 ; i++)
super.droite();
}
public void demiTour(){
super.droite();
super.droite();
}
}
Exercice 2
public interface IOperation {
Object plus (Object obj);
Object moins (Object obj);
}
2
public Object moins (Object c){
Complexe com = (Complexe) c;
return new Complexe (this.re - com.re, this.im - com.im);
}
public String affiche() {
if (this.re == 0) {
if (this.im == 0)
return 0+"";
else
return this.im+" i";
}
else {
if(this.im> 0)
return this.re+" + "+this.im+" i";
else if(this.im< 0)
return this.re+" "+this.im+" i";
else
return this.re+"";
}
}
}
3
Complexe c3,c4 ;
c3 =(Complexe)(tab[1].plus(tab[0]));
c4=(Complexe)(tab[1].moins(tab[0]));
System.out.println("tab[1] + tab[0] = " +c3.affiche());
System.out.println("tab[1] - tab[0] = " +c4.affiche());
Reel r3;
r3=(Reel)(tab[2].plus(tab[3]));
System.out.println("tab[2] + tab[3] = " +r3.affiche());
}
}