Browse Source

added tp4 (OOP)

master
Georges-Emile Bihr 11 months ago
parent
commit
dfec574d1b
  1. 21
      unistra/l2_s1a_2021/poo/tp4/implementation/Carre.java
  2. 29
      unistra/l2_s1a_2021/poo/tp4/implementation/Rectangle.java
  3. 26
      unistra/l2_s1a_2021/poo/tp4/implementation/TestHeritageImplementation.java
  4. 15
      unistra/l2_s1a_2021/poo/tp4/modelisation/Carre.java
  5. 32
      unistra/l2_s1a_2021/poo/tp4/modelisation/Rectangle.java
  6. 22
      unistra/l2_s1a_2021/poo/tp4/modelisation/TestHeritageModelisation.java
  7. 20
      unistra/l2_s1a_2021/poo/tp4/polymorphisme/Carre.java
  8. 17
      unistra/l2_s1a_2021/poo/tp4/polymorphisme/FormeGeo.java
  9. 16
      unistra/l2_s1a_2021/poo/tp4/polymorphisme/Rectangle.java
  10. 22
      unistra/l2_s1a_2021/poo/tp4/polymorphisme/TestHeritage.java

21
unistra/l2_s1a_2021/poo/tp4/implementation/Carre.java

@ -0,0 +1,21 @@
public class Carre {
protected double dx;
public Carre(double c) {
}
public double getCote() {
return this.dx;
}
public void setCote(double c) {
this.dx = c;
}
public double perimetre() {
return (this.dx*4);
}
public double surface() {
return this.dx * this.dx;
}
public String toString() {
return "Je suis un carre.";
}
}

29
unistra/l2_s1a_2021/poo/tp4/implementation/Rectangle.java

@ -0,0 +1,29 @@
public class Rectangle extends Carre {
protected double dy;
public Rectangle(double dx, double dy) {
super(dx);
this.setdy(dy);
}
public double getdx() {
return this.getCote();
}
public double getdy() {
return this.dy;
}
public void setdx(double dx) {
this.setCote(dx);
}
public void setdy(double dy) {
this.dy = dy;
}
public double perimetre() {
return (this.dx*2) + (this.dy*2);
}
public double surface() {
return this.dx * this.dy;
}
public String toString() {
return "Je suis un rectangle.";
}
}

26
unistra/l2_s1a_2021/poo/tp4/implementation/TestHeritageImplementation.java

@ -0,0 +1,26 @@
public class TestHeritageImplementation {
static final int TSIZE = 5;
public static void main(String[] args) {
Carre[] tableau;
tableau = new Carre[TSIZE];
tableau[0] = new Rectangle(5, 2.25);
tableau[1] = new Carre(10);
tableau[2] = new Carre(8.5);
tableau[3] = new Rectangle(2.5, 6);
tableau[4] = new Rectangle(3.3, 7.1);
for (int i = 0; i < 5; i++) {
System.out.printf("%s J'ai pour abscisse et ordonnee : ", tableau[i]);
if (tableau[i] instanceof Rectangle) {
System.out.printf("%.1f, %.1f\n", tableau[i].getdx(), tableau[i].getdy());
} else {
System.out.printf("%.1f, %.1f\n", tableau[i].getCote(), tableau[i].getCote());
}
System.out.printf("Mon perimetre et ma surface sont respectivement : ");
System.out.printf("%.2f, %.2f\n", tableau[i].perimetre(), tableau[i].surface());
System.out.println();
}
}
}

15
unistra/l2_s1a_2021/poo/tp4/modelisation/Carre.java

@ -0,0 +1,15 @@
public class Carre extends Rectangle {
public Carre(double c) {
super(c, c);
}
public double getCote() {
return this.dx;
}
public void setCote(double c) {
this.setdx(c);
this.setdy(c);
}
public String toString() {
return "Je suis un carre.";
}
}

32
unistra/l2_s1a_2021/poo/tp4/modelisation/Rectangle.java

@ -0,0 +1,32 @@
public class Rectangle {
protected double dx;
protected double dy;
public Rectangle(double dx, double dy) {
//this.dx = dx;
//this.dy = dy;
this.setdx(dx);
this.setdy(dy);
}
public double getdx() {
return this.dx;
}
public double getdy() {
return this.dy;
}
public void setdx(double dx) {
this.dx = dx;
}
public void setdy(double dy) {
this.dy = dy;
}
public double perimetre() {
return (this.dx*2) + (this.dy*2);
}
public double surface() {
return this.dx * this.dy;
}
public String toString() {
return "Je suis un rectangle.";
}
}

22
unistra/l2_s1a_2021/poo/tp4/modelisation/TestHeritageModelisation.java

@ -0,0 +1,22 @@
public class TestHeritageModelisation {
static final int TSIZE = 5;
public static void main(String[] args) {
Rectangle[] tableau;
tableau = new Rectangle[TSIZE];
tableau[0] = new Rectangle(5, 2.25);
tableau[1] = new Carre(10);
tableau[2] = new Carre(8.5);
tableau[3] = new Rectangle(2.5, 6);
tableau[4] = new Rectangle(3.3, 7.1);
for (int i = 0; i < 5; i++) {
System.out.printf("%s J'ai pour abscisse et ordonnee : ", tableau[i]);
System.out.printf("%.1f, %.1f\n", tableau[i].getdx(), tableau[i].getdy());
System.out.printf("Mon perimetre et ma surface sont respectivement : ");
System.out.printf("%.2f, %.2f\n", tableau[i].perimetre(), tableau[i].surface());
System.out.println();
}
}
}

20
unistra/l2_s1a_2021/poo/tp4/polymorphisme/Carre.java

@ -0,0 +1,20 @@
public class Carre extends FormeGeo {
public Carre(double c) {
this.setCote(c);
}
public double getCote() {
return this.getdx();
}
public void setCote(double c) {
this.setdx(c);
}
public double perimetre() {
return (this.dx*4);
}
public double surface() {
return this.dx * this.dx;
}
public String toString() {
return "Je suis un carre.";
}
}

17
unistra/l2_s1a_2021/poo/tp4/polymorphisme/FormeGeo.java

@ -0,0 +1,17 @@
public abstract class FormeGeo {
protected double dx, dy;
double getdx() {
return this.dx;
}
double getdy() {
return this.dy;
}
void setdx(double dx) {
this.dx = dx;
}
void setdy(double dy) {
this.dy = dy;
}
abstract double perimetre();
abstract double surface();
}

16
unistra/l2_s1a_2021/poo/tp4/polymorphisme/Rectangle.java

@ -0,0 +1,16 @@
public class Rectangle extends FormeGeo {
public Rectangle(double dx, double dy) {
this.setdx(dx);
this.setdy(dy);
}
public double perimetre() {
return (this.dx*2) + (this.dy*2);
}
public double surface() {
return this.dx * this.dy;
}
public String toString() {
return "Je suis un rectangle.";
}
}

22
unistra/l2_s1a_2021/poo/tp4/polymorphisme/TestHeritage.java

@ -0,0 +1,22 @@
public class TestHeritage {
static final int TSIZE = 5;
public static void main(String[] args) {
FormeGeo[] tableau;
tableau = new Carre[TSIZE];
tableau[0] = new Rectangle(5, 2.25);
tableau[1] = new Carre(10);
tableau[2] = new Carre(8.5);
tableau[3] = new Rectangle(2.5, 6);
tableau[4] = new Rectangle(3.3, 7.1);
for (int i = 0; i < 5; i++) {
System.out.printf("%s J'ai pour abscisse et ordonnee : ", tableau[i]);
System.out.printf("%.1f, %.1f\n", tableau[i].getdx(), tableau[i].getdy());
System.out.printf("Mon perimetre et ma surface sont respectivement : ");
System.out.printf("%.2f, %.2f\n", tableau[i].perimetre(), tableau[i].surface());
System.out.println();
}
}
}
Loading…
Cancel
Save