Vous êtes sur la page 1sur 4

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){
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){};
}

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++)
super.avance();
}
public void gauche(){
for (int i = 0 ; i < 3 ; i++)
super.droite();
}
public void demiTour(){
super.droite();
super.droite();
}
}

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)
r.afficher();
}
}

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){
this.re = re;
this.im = im;
}
public Object plus(Object c){
Complexe com = (Complexe)c;
return new Complexe (this.re + com.re, this.im + com.im);
}

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+"";
}
}
}

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)
System.out.println("tab["+i+"] = " +((Complexe) tab[i]).affiche());
else
System.out.println("tab["+i+"] = " +((Reel) tab[i]).affiche());
}

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());
}
}

Vous aimerez peut-être aussi