Exercice 1 : Classe rectangle et Point // td1 exercice 1 // fichier Point.java public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return ("("+ x +"," + y + ")"); } } // fichier Rectangle.java public class Rectangle { private Point infGauche; private Point supDroit; public Rectangle(int x1, int y1, int x2, int y2) { infGauche = new Point(x1,y1); supDroit = new Point(x2,y2); } public int surface() { return (supDroit.getX() - infGauche.getX())* (supDroit.getY() - infGauche.getY()); } public String toString(){ return ("inferieur gauche : " + infGauche + " superieur droit : " + supDroit); } } // fichier surfRectangle.java public class surfRectangle { public static void main(String args[]) { Rectangle r = new Rectangle(0,0,3,4); System.out.println("r " + r + " surface: " + r.surface()); } } //td2 exercice 2 // fichier Point.java public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public void translater(int x, int y) { this.x = this.x + x; this.y = this.y + y; } public int compareTo(Point p){ if ((this.x == p.x) && (this.y == p.y)){ return 0; } else if (this.x < p.x){ return -1; } return 1; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return ("("+ x +"," + y + ")"); } } //fichier Rectangle.java public class Rectangle { private Point infGauche; private Point supDroit; public Rectangle() { infGauche = new Point(0,0); supDroit = new Point(1,1); } public Rectangle(int x1, int y1, int x2, int y2) { infGauche = new Point(x1,y1); supDroit = new Point(x2,y2); } public Rectangle(Point p1, Point p2) { infGauche = p1; supDroit = p2; } public int surface() { return (supDroit.getX() - infGauche.getX())* (supDroit.getY() - infGauche.getY()); } public int compareTo(Rectangle r) { if ((this.infGauche.compareTo(r.infGauche) == 0 ) && (this.supDroit.compareTo(r.supDroit) == 0)){ return 0; } return (this.infGauche.compareTo(r.infGauche) ); } public void translater (int a, int b){ infGauche.setX(infGauche.getX() + a); infGauche.setY(infGauche.getY() + b); supDroit.setX(supDroit.getX() + a); supDroit.setY(supDroit.getY() + b); } public String toString(){ return ("inferieur gauche : " + infGauche + " superieur droit : " + supDroit); } } //fichier Main.java public class Test { public static void main(String args[]) { Point p1 = new Point(2,2); Point p2 = new Point(4,6); Rectangle r = new Rectangle(p1,p2); System.out.println("r " + r + " surface: " + r.surface()); // 8 p1.translater(2,3); // La modification de p1 change la surface du rectangle System.out.println("r " + r + " surface: " + r.surface()); // 0 p1 = new Point(2,2); p2 = new Point(4,6); r = new Rectangle(p1.getX(), p1.getY(), p2.getX(), p2.getY()); System.out.println("r " + r + " surface: " + r.surface()); // 8 p1.translater(2,3); // La modification de p1 n'a pas d'effet sur la surface du rectangle System.out.println("r " + r + " surface: " + r.surface()); // 8 p1 = new Point(2,2); p2 = new Point(4,6); r = new Rectangle(p1.getX(), p1.getY(), p2.getX(), p2.getY()); System.out.println("r " + r + " surface: " + r.surface()); // 8 r.translater(2,3); // La translation de r n'a pas d'effet sur la surface du rectangle // mais ces deux point ont ete modifie System.out.println("r " + r + " surface: " + r.surface()); // 8 // p1 n'est pas modifie System.out.println("p1 " + p1); // 8 } }