TP08 Polymorphisme

This commit is contained in:
HORVILLE 2022-03-14 17:26:22 +01:00
parent ae43ed1b4f
commit f22fb9811a
22 changed files with 200 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,18 @@
public class Moyenne {
private int count;
private double sum;
public Moyenne() {
count = 0;
sum = 0;
}
public void add(Number n) {
count++;
sum += n.doubleValue();
}
public double getAverage() {
return sum / (double)count;
}
}

Binary file not shown.

View File

@ -0,0 +1,12 @@
public class TestMoyenne {
public static void main(String[] args) {
Moyenne m = new Moyenne();
m.add(1);
m.add(1.2f);
m.add(1.52d);
m.add(2);
System.out.println(m.getAverage());
}
}

Binary file not shown.

View File

@ -0,0 +1,26 @@
import java.awt.Point;
public class Etoile implements ProducteurDePoints {
private static final int xCentre = 100;
private static final int yCentre = 100;
private static final double rayon = 90.0;
private static final double angleDepart = Math.PI/4.0;
private static final double angleIncrement = (4.0*Math.PI)/5.0;
private double numero;
public Etoile() {
this.numero = 0.0;
}
public Point suivant() {
Point p = null;
if (this.numero < 6.0) {
double angle = Etoile.angleDepart+this.numero*Etoile.angleIncrement;
p = new Point((int) (Etoile.rayon*Math.cos(angle)),
(int) (Etoile.rayon*Math.sin(angle)));
p.translate(Etoile.xCentre, Etoile.yCentre);
this.numero++;
} else {
this.numero = 0.0;
}
return p;
}
}

Binary file not shown.

View File

@ -0,0 +1,39 @@
import java.awt.*;
import javax.swing.*;
public class Polyligne extends JComponent {
Point p1;
Point p2;
ProducteurDePoints pp;
public Polyligne(ProducteurDePoints arg) {
super();
pp = arg;
}
@Override
protected void paintComponent(Graphics brush) {
Graphics newBrush = brush.create();
if (this.isOpaque()) {
newBrush.setColor(this.getBackground());
newBrush.fillRect(0, 0, this.getWidth(), this.getHeight());
}
newBrush.setColor(Color.BLACK);
System.out.println("Test");
while (true) {
if (p1 != null) {
newBrush.drawLine(p1.x, p1.y, p2.x, p2.y);
p1 = p2;
p2 = pp.suivant();
if (p2 == null) break;
} else {
p1 = pp.suivant();
p2 = pp.suivant();
}
}
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
import java.awt.Point;
public interface ProducteurDePoints {
Point suivant();
}

Binary file not shown.

View File

@ -0,0 +1,34 @@
import java.awt.Point;
public class Spirale implements ProducteurDePoints {
private static final int centreX = 100;
private static final int centreY = 100;
private static final double rayon = 90.0;
private static final double radiusIncrement = -0.1;
private static final double angleDepart = 0.0;
private static final double angleIncrement = Math.PI / 30;
private double angle;
private double curRadius;
public Spirale() {
angle = angleDepart;
curRadius = rayon;
}
public Point suivant() {
Point p = null;
angle += angleIncrement;
curRadius += radiusIncrement;
if (curRadius > 0.0) {
p = new Point(
(int)(Math.cos(angle) * curRadius),
(int)(Math.sin(angle) * curRadius)
);
p.translate(centreX, centreY);
}
return p;
}
}

Binary file not shown.

View File

@ -0,0 +1,20 @@
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(250, 250);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
Spirale e = new Spirale();
Polyligne pl = new Polyligne(e);
pl.setSize(230, 230);
pl.setLocation(0, 0);
f.add(pl);
f.setVisible(true);
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
public class Moto extends Vehicule {
public Moto() {
this.wheels = 2;
this.type = "Moto";
}
}

Binary file not shown.

View File

@ -0,0 +1,12 @@
public abstract class Vehicule {
protected int wheels;
protected String type;
public int nbRoues() {
return this.wheels;
}
public String sorte() {
return this.type;
}
}

Binary file not shown.

View File

@ -0,0 +1,22 @@
import javax.swing.JOptionPane;
public class Vehicules {
public static void main(String[] args) {
Vehicule v;
Object[] choix = {"Voiture", "Moto"};
int reponse = JOptionPane.showOptionDialog(null,
"Quel v\u00E9hicule choisissez-vous ?",
"Question",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choix,
null);
if (reponse == 0)
v = new Voiture();
else
v = new Moto();
System.out.println("Une "+v.sorte()+" poss\u00E8de "+v.nbRoues()+" roues.");
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
public class Voiture extends Vehicule {
public Voiture() {
this.wheels = 4;
this.type = "Voiture";
}
}