TP08 Polymorphisme
This commit is contained in:
parent
ae43ed1b4f
commit
f22fb9811a
BIN
APL2.1/TP08/Moyenne/Moyenne.class
Normal file
BIN
APL2.1/TP08/Moyenne/Moyenne.class
Normal file
Binary file not shown.
18
APL2.1/TP08/Moyenne/Moyenne.java
Normal file
18
APL2.1/TP08/Moyenne/Moyenne.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Moyenne/TestMoyenne.class
Normal file
BIN
APL2.1/TP08/Moyenne/TestMoyenne.class
Normal file
Binary file not shown.
12
APL2.1/TP08/Moyenne/TestMoyenne.java
Normal file
12
APL2.1/TP08/Moyenne/TestMoyenne.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Polyligne/Etoile.class
Normal file
BIN
APL2.1/TP08/Polyligne/Etoile.class
Normal file
Binary file not shown.
26
APL2.1/TP08/Polyligne/Etoile.java
Normal file
26
APL2.1/TP08/Polyligne/Etoile.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Polyligne/Polyligne.class
Normal file
BIN
APL2.1/TP08/Polyligne/Polyligne.class
Normal file
Binary file not shown.
39
APL2.1/TP08/Polyligne/Polyligne.java
Normal file
39
APL2.1/TP08/Polyligne/Polyligne.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Polyligne/ProducteurDePoints.class
Normal file
BIN
APL2.1/TP08/Polyligne/ProducteurDePoints.class
Normal file
Binary file not shown.
5
APL2.1/TP08/Polyligne/ProducteurDePoints.java
Normal file
5
APL2.1/TP08/Polyligne/ProducteurDePoints.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
|
public interface ProducteurDePoints {
|
||||||
|
Point suivant();
|
||||||
|
}
|
BIN
APL2.1/TP08/Polyligne/Spirale.class
Normal file
BIN
APL2.1/TP08/Polyligne/Spirale.class
Normal file
Binary file not shown.
34
APL2.1/TP08/Polyligne/Spirale.java
Normal file
34
APL2.1/TP08/Polyligne/Spirale.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Polyligne/Test.class
Normal file
BIN
APL2.1/TP08/Polyligne/Test.class
Normal file
Binary file not shown.
20
APL2.1/TP08/Polyligne/Test.java
Normal file
20
APL2.1/TP08/Polyligne/Test.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Véhicules/Moto.class
Normal file
BIN
APL2.1/TP08/Véhicules/Moto.class
Normal file
Binary file not shown.
6
APL2.1/TP08/Véhicules/Moto.java
Normal file
6
APL2.1/TP08/Véhicules/Moto.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Moto extends Vehicule {
|
||||||
|
public Moto() {
|
||||||
|
this.wheels = 2;
|
||||||
|
this.type = "Moto";
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Véhicules/Vehicule.class
Normal file
BIN
APL2.1/TP08/Véhicules/Vehicule.class
Normal file
Binary file not shown.
12
APL2.1/TP08/Véhicules/Vehicule.java
Normal file
12
APL2.1/TP08/Véhicules/Vehicule.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Véhicules/Vehicules.class
Normal file
BIN
APL2.1/TP08/Véhicules/Vehicules.class
Normal file
Binary file not shown.
22
APL2.1/TP08/Véhicules/Vehicules.java
Normal file
22
APL2.1/TP08/Véhicules/Vehicules.java
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
BIN
APL2.1/TP08/Véhicules/Voiture.class
Normal file
BIN
APL2.1/TP08/Véhicules/Voiture.class
Normal file
Binary file not shown.
6
APL2.1/TP08/Véhicules/Voiture.java
Normal file
6
APL2.1/TP08/Véhicules/Voiture.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Voiture extends Vehicule {
|
||||||
|
public Voiture() {
|
||||||
|
this.wheels = 4;
|
||||||
|
this.type = "Voiture";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user