You are on page 1of 2

Atelier POO – Correction des TPs

TP N° 3 Classes et objets

Exercice 1 et 2 - Point
public class Point {
int x,y;
void decrire(){
System.out.println("x="+x+" y="+y);
}
Point(int a,int b){
x=a;
y=b;
}
Point(){
x=0;
y=0;
}
Point(Point p){
this.x=p.x;
this.y=p.y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
void translate (int dx,int dy){
x+=dx;
y+=dy;
}
boolean isSameAs(Point p){
if(this.x==p.x && this.y==p.y)
return true;
else
return false;
}
public static void main(String[] args) {
Point p=new Point(3,4);
p.decrire();
Point q=new Point(1,2);
Point p2=new Point(1,2);
System.out.println(q.isSameAs(p2));

}}
Exercice 3 - Circle

public class Circle {


int rayon;

Imene Sghaier 32
Atelier POO – Correction des TPs

Point centre;
public Circle(int rayon, Point centre) {
this.rayon = rayon;
this.centre = centre;
}
void decrire(){
centre.decrire();
System.out.println("rayon="+rayon);
}
void translate(int dx,int dy){
centre.translate(dx, dy);
}
boolean isSameAs(Circle c){
if(c.centre.isSameAs(this.centre) && this.rayon==c.rayon)
return true;
else
return false;
}
public boolean contains(Point p){
if(p.x-centre.x==rayon || p.y-centre.y==rayon)
return true;
else
return false;
}
public static void main(String[] args) {
Point p=new Point(1,2);
Circle c=new Circle(1,p);
Circle c2=new Circle(2,p);
c2.translate(1,1);
c.decrire() ;
c2.decrire() ;

}}

Imene Sghaier 33

You might also like