Université Mohammed Premier, Oujda Module POO-Java
Faculté des sciences Filière SMI - Semestre 5
Département d’Informatique Année universitaire 2023/2024
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){
[Link] = nom;
this.x = x;
this.y = y;
[Link] = direction;
}
public void avance(){
if ([Link]("Est"))
x++;
else if ([Link]("Ouest"))
x--;
else if ([Link]("Nord"))
y++;
else
y--;
}
public void droite(){
if ([Link]("Nord"))
direction = "Est";
else if ([Link]("Est"))
direction = "Sud";
else if ([Link]("Sud"))
direction ="Ouest";
else
direction = "Nord";
}
public void afficher(){
[Link]("Nom : " + nom);
[Link]("Position : ("+ x + "," + y +"));
[Link](Direction : " +direction);
}
public void gauche(){};
public void demiTour(){};
public void avance(int pas){};
}
public class RobotNG extends Robot {
public RobotNG(String nom, int x, int y, String direction){
super(nom, x, y, direction);
}
1
public void avance(int nbrePas){
for (int i = 0 ; i < nbrePas ; i++)
[Link]();
}
public void gauche(){
for (int i = 0 ; i < 3 ; i++)
[Link]();
}
public void demiTour(){
[Link]();
[Link]();
}
}
public class RobotExam {
public static void main(String[] args) {
Robot[] tableau = new RobotNG[3] ;
tableau[0] = new RobotNG("a",2, 4, "Nord");
tableau[0].avance();
tableau[0].droite();
tableau[1] = new RobotNG("b",22, 13, "Ouest");
tableau[1].avance(3);
tableau[1].droite();
tableau[2] = new RobotNG("c",12, 31, "Sud");
tableau[2].avance(7);
tableau[2].gauche();
tableau[2].demiTour();
for (Robot r : tableau)
[Link]();
}
}
Exercice 2
public interface IOperation {
Object plus (Object obj);
Object moins (Object obj);
}
public abstract class Affichage {
public abstract String affiche();
}
public class Complexe extends Affichage implements IOperation {
private double im;
private double re;
public Complexe (double re , double im){
[Link] = re;
[Link] = im;
}
public Object plus(Object c){
Complexe com = (Complexe)c;
return new Complexe ([Link] + [Link], [Link] + [Link]);
}
2
public Object moins (Object c){
Complexe com = (Complexe) c;
return new Complexe ([Link] - [Link], [Link] - [Link]);
}
public String affiche() {
if ([Link] == 0) {
if ([Link] == 0)
return 0+"";
else
return [Link]+" i";
}
else {
if([Link]> 0)
return [Link]+" + "+[Link]+" i";
else if([Link]< 0)
return [Link]+" "+[Link]+" i";
else
return [Link]+"";
}
}
}
public class Reel extends Affichage implements IOperation {
private double x;
public Reel(double x){
this.x=x;
}
public Object plus(Object obj) {
Reel reel = (Reel) obj;
return new Reel(this.x+reel.x);
}
public Object moins(Object obj) {
Reel reel = (Reel) obj;
return new Reel(this.x-reel.x);
}
public String affiche() {
return x+"";
}
}
public class Test {
public static void main (String[] args){
IOperation[] tab = new IOperation[4];
tab[0] = new Complexe(2,2) ;
tab[1] = new Complexe(-3,-4) ;
tab[2]=new Reel(3.4);
tab[3]=new Reel(3.89);
for (int i=0;i<4;i++) {
if (i<2)
[Link]("tab["+i+"] = " +((Complexe) tab[i]).affiche());
else
[Link]("tab["+i+"] = " +((Reel) tab[i]).affiche());
}
3
Complexe c3,c4 ;
c3 =(Complexe)(tab[1].plus(tab[0]));
c4=(Complexe)(tab[1].moins(tab[0]));
[Link]("tab[1] + tab[0] = " +[Link]());
[Link]("tab[1] - tab[0] = " +[Link]());
Reel r3;
r3=(Reel)(tab[2].plus(tab[3]));
[Link]("tab[2] + tab[3] = " +[Link]());
}
}