From e847dce13a75a33f7a177a6bfabaf5eef2609a10 Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Wed, 20 May 2020 18:47:15 +0200 Subject: [PATCH] TD1 src fini --- README.md | 4 +- src/TD1/HelloWorld.java | 2 +- src/TD1/point/MainTestPoint.java | 111 +++++++++++++++++++++ src/TD1/point/Point.java | 140 +++++++++++++-------------- src/TD1/point/PointTest.java | 14 --- src/TD1/point/TestPoint.java | 76 --------------- src/TD4/pointcolore/PointColore.java | 2 +- src/TD5/pointcolore/PointColore.java | 2 +- tests/TD1/TestPoint.java | 5 + 9 files changed, 189 insertions(+), 167 deletions(-) create mode 100644 src/TD1/point/MainTestPoint.java delete mode 100644 src/TD1/point/PointTest.java delete mode 100644 src/TD1/point/TestPoint.java create mode 100644 tests/TD1/TestPoint.java diff --git a/README.md b/README.md index 5ef5056..5627b7c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # M213-Bases-de-la-programmation-oriente-objet -M213-Bases-de-la-programmation-oriente-objet + +Ce repo contient le contenu du module qui m'a appris à développer avec une approche objet. +Il a été initialisé avec mes codes originaux puis refait entièrement de manière plus propre et professionelle à partir du 20/05/2020. diff --git a/src/TD1/HelloWorld.java b/src/TD1/HelloWorld.java index 74e092f..20b7c59 100644 --- a/src/TD1/HelloWorld.java +++ b/src/TD1/HelloWorld.java @@ -4,6 +4,6 @@ public class HelloWorld { public static void main(String[] args) { - System.out.println("\nHelloWord ! :)"); + System.out.println("HelloWord ! :)"); } } \ No newline at end of file diff --git a/src/TD1/point/MainTestPoint.java b/src/TD1/point/MainTestPoint.java new file mode 100644 index 0000000..ebc7e63 --- /dev/null +++ b/src/TD1/point/MainTestPoint.java @@ -0,0 +1,111 @@ +package TD1.point; + +public class MainTestPoint { + public static void main(String[] args) + { + Point O = new Point(); + Point I = new Point(1.0, 0.0); + Point J = new Point(0.0, 1.0); + Point A = new Point(1.0, 3.5); + Point B = new Point(8.0, 20.0); + Point C = new Point(-2.0, 3.0); + Point D = new Point(1.0, 1.0); + + Point A2 = new Point(A); + Point B2 = new Point(B); + Point C2 = new Point(C); + Point O2 = new Point(D); + + Point A3 = new Point(A); + Point B3 = new Point(B); + Point C3 = new Point(C); + Point O3 = new Point(D); + + System.out.println("\n===== Les toString() ====="); + System.out.println(" O = " + O.toString()); + System.out.println(" I = " + I.toString()); + System.out.println(" J = " + J.toString()); + System.out.println(" A = " + A.toString()); + System.out.println(" B = " + B.toString()); + System.out.println(" C = " + C.toString()); + System.out.println(" D = " + D.toString()); + + + System.out.println("\n===== Les getDistance() ====="); + System.out.println("La distance O - I est de " + O.getDistance(I)); + System.out.println("La distance O - J est de " + O.getDistance(J)); + System.out.println("La distance J - I est de " + J.getDistance(I)); + System.out.println("La distance A - B est de " + A.getDistance(B)); + System.out.println("La distance B - A est de " + B.getDistance(A)); + System.out.println("La distance A - C est de " + A.getDistance(C)); + System.out.println("La distance C - A est de " + C.getDistance(A)); + System.out.println("La distance O - D est de " + O.getDistance(D)); + + + System.out.println("\n===== Les getX() et getY() ====="); + System.out.println("Le getX de A est " + A.getX()); + System.out.println("Le getY de A est " + A.getY()); + + + System.out.println("\n===== Les ProjX() et ProjY() ====="); + System.out.println("Le ProjX de A est " + A.projX()); + System.out.println("Le ProjY de A est " + A.projY()); + System.out.println("Le ProjX de B est " + B.projX()); + System.out.println("Le ProjY de B est " + B.projY()); + System.out.println("Le ProjX de O est " + O.projX()); + System.out.println("Le ProjY de O est " + O.projY()); + System.out.println("Le ProjX de I est " + I.projX()); + System.out.println("Le ProjY de I est " + I.projY()); + System.out.println("Le ProjX de J est " + J.projX()); + System.out.println("Le ProjY de J est " + J.projY()); + + + System.out.println("\n===== Les equals() ====="); + System.out.println("A et A : " + A.equals(A)); + System.out.println("O et O : " + O.equals(O)); + System.out.println("I et J : " + I.equals(J)); + System.out.println("J et I : " + J.equals(I)); + System.out.println("B et B : " + B.equals(B)); + System.out.println("C et C : " + C.equals(C)); + + + A2.setLocation(5., 2.); + B2.setLocation(3., -2.); + C2.setLocation(6., 5.); + O2.setLocation(-5., 8.); + + + + A3.translate(5, 2); + B3.translate(3, -2); + C3.translate(6, 5); + O3.translate(-5, 8); + + System.out.println("\nAffichage des points"); + System.out.println("O : " + O.toString()); + System.out.println("I : " + I.toString()); + System.out.println("J : " + J.toString()); + System.out.println("A : " + A.toString()); + System.out.println("B : " + B.toString()); + System.out.println("C : " + C.toString()); + System.out.println("D : " + D.toString()); + + System.out.println(); + + System.out.println("A2 : " + A2.toString()); + System.out.println("B2 : " + B2.toString()); + System.out.println("C2 : " + C2.toString()); + System.out.println("O2 : " + O2.toString()); + + System.out.println(); + + System.out.println("A3 : " + A3.toString()); + System.out.println("B3 : " + B3.toString()); + System.out.println("C3 : " + C3.toString()); + System.out.println("O3 : " + O3.toString()); + + + + + } +} diff --git a/src/TD1/point/Point.java b/src/TD1/point/Point.java index 8cd5e4d..4653ace 100644 --- a/src/TD1/point/Point.java +++ b/src/TD1/point/Point.java @@ -2,25 +2,34 @@ package TD1.point; public class Point { - //attributs - public double x; - public double y; + private double x; + private double y; - - //constructeurs - public Point() - { - x = 0; - y = 0; + public Point() { + this(0, 0); } - public Point(double x, double y) - { + public Point(Point p) { + this(p.getX(), p.getY()); + } + + public Point(int x, int y) { + this((double)x, (double)y); + } + + public Point(double x, double y) { this.x = x; this.y = y; } + + public Point projX() { + return new Point(this.getX(), 0); + } + + public Point projY() { + return new Point(0, this.getY()); + } - //methode d'instance public String toString() { return "(X = " + this.x + " - Y = " + this.y + ")"; @@ -28,73 +37,58 @@ public class Point public double getDistance(Point p) { - double result; - result = Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)); - return result; + return Math.sqrt(Math.pow(this.getX() - p.x, 2) + Math.pow(this.getY() - p.y, 2)); } - - //accesseur en lecture - public double getX() { + + @Override + public boolean equals(Object obj) { + Point p = (Point)obj; + if (this.x == p.x && this.y == p.y) { + return true; + } else { + return false; + } + } + + public Point clone() { + return this; + } + + public void displayLocation() { + System.out.println("x = " + this.getX() + " , y = " + this.getY() + "."); + } + + public Point getLocation() { + return this; + } + + public void setLocation(Point p) { + this.setX(p.getX()); + this.setY(p.getY()); + } + + public void setLocation(double x, double y) { + this.setLocation(new Point(x, y)); + } + + public void translate (int dx, int dy) { + this.setX(this.getX() + dx); + this.setY(this.getY() + dy); + } + + public double getX() { return x; } + public void setX(double x) { + this.x = x; + } + public double getY() { return y; } - public Point projX() - { - return new Point(this.x, 0.); - } - - public Point projY() - { - return new Point(0., this.y); - } - - public boolean equals (Object obj) - { - Point p = (Point)obj; - if (this.x == p.x && this.y == p.y) - return true; - else return false; - } - - public Point clone() - { - return new Point(this.x, this.y); - } - - public void getLocation() - { - System.out.println("x = " + this.x + " , y = " + this.y + "."); - } - - public void setLocation(Point p) - { - p.x = this.x; - p.y = this.y; - } - public void setLocation(double x, double y) - { - x = this.x; - y = this.y; - } - - public void translate(int dx, int dy) - { - this.x = this.x+dx; - this.y = this.y+dy; - } - - //getters and setters - public void setX(double x) { - this.x = x; - } - - public void setY(double y) { - this.y = y; - } - - + public void setY(double y) { + this.y = y; + } } diff --git a/src/TD1/point/PointTest.java b/src/TD1/point/PointTest.java deleted file mode 100644 index c25b919..0000000 --- a/src/TD1/point/PointTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package TD1.point; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class PointTest { - - @Test - void test() { - fail("Not yet implemented"); - } - -} diff --git a/src/TD1/point/TestPoint.java b/src/TD1/point/TestPoint.java deleted file mode 100644 index 6b57be0..0000000 --- a/src/TD1/point/TestPoint.java +++ /dev/null @@ -1,76 +0,0 @@ -package TD1.point; - -public class TestPoint { - public static void main(String[] args) - { - Point O = new Point(); - Point I = new Point(1.0, 0.0); - Point J = new Point(0.0, 1.0); - Point A = new Point(1.0, 3.5); - Point B = new Point(8.0, 20.0); - Point C = new Point(-2.0, 3.0); - Point D = new Point(1.0, 1.0); - - - System.out.println(" O = " + O.toString()); - System.out.println(" I = " + I.toString()); - System.out.println(" J = " + J.toString()); - System.out.println(" A = " + A.toString()); - System.out.println(" B = " + B.toString()); - System.out.println(" C = " + C.toString()); - System.out.println(" D = " + D.toString()); - - System.out.println("La distance O - I est de " + O.getDistance(I)); - System.out.println("La distance O - J est de " + O.getDistance(J)); - System.out.println("La distance J - I est de " + J.getDistance(I)); - System.out.println("La distance A - B est de " + A.getDistance(B)); - System.out.println("La distance B - A est de " + B.getDistance(A)); - System.out.println("La distance A - C est de " + A.getDistance(C)); - System.out.println("La distance C - A est de " + C.getDistance(A)); - System.out.println("La distance O - D est de " + O.getDistance(D)); - - System.out.println("Le getX de A est " + A.getX()); - System.out.println("Le getY de A est " + A.getY()); - - System.out.println("Le ProjX de A est " + A.projX()); - System.out.println("Le ProjY de A est " + A.projY()); - System.out.println("Le ProjX de B est " + B.projX()); - System.out.println("Le ProjY de B est " + B.projY()); - System.out.println("Le ProjX de O est " + O.projX()); - System.out.println("Le ProjY de O est " + O.projY()); - System.out.println("Le ProjX de I est " + I.projX()); - System.out.println("Le ProjY de I est " + I.projY()); - System.out.println("Le ProjX de J est " + J.projX()); - System.out.println("Le ProjY de J est " + J.projY()); - - System.out.println(A.equals(A)); - System.out.println(O.equals(O)); - System.out.println(I.equals(J)); - System.out.println(J.equals(I)); - System.out.println(B.equals(B)); - System.out.println(C.equals(C)); - - - /*A.setLocation(5., 2.); - B.setLocation(3., -2.); - C.setLocation(6., 5.); - O.setLocation(-5., 8.);*/ - - A.translate(5, 2); - B.translate(3, -2); - C.translate(6, 5); - O.translate(-5, 8); - - - System.out.println(A.toString()); - System.out.println(B.toString()); - System.out.println(C.toString()); - System.out.println(O.toString()); - - - - - - - } -} diff --git a/src/TD4/pointcolore/PointColore.java b/src/TD4/pointcolore/PointColore.java index 26114b7..9eb26b3 100644 --- a/src/TD4/pointcolore/PointColore.java +++ b/src/TD4/pointcolore/PointColore.java @@ -48,7 +48,7 @@ public class PointColore extends Point { public boolean equals (Object obj) { Point p = (Point)obj; - if (super.x == p.x && super.y == p.y) + if (super.getX() == p.getX() && super.getY() == p.getY()) return true; else return false; } diff --git a/src/TD5/pointcolore/PointColore.java b/src/TD5/pointcolore/PointColore.java index 3780ced..251e02f 100644 --- a/src/TD5/pointcolore/PointColore.java +++ b/src/TD5/pointcolore/PointColore.java @@ -48,7 +48,7 @@ public class PointColore extends Point { public boolean equals (Object obj) { Point p = (Point)obj; - if (super.x == p.x && super.y == p.y) + if (super.getX() == p.getX() && super.getY() == p.getY()) return true; else return false; } diff --git a/tests/TD1/TestPoint.java b/tests/TD1/TestPoint.java new file mode 100644 index 0000000..1a394ad --- /dev/null +++ b/tests/TD1/TestPoint.java @@ -0,0 +1,5 @@ +package TD1; + +public class TestPoint { + +}