This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
2013_2014:s2:td:corrections:td_classes-code [2014/02/15 21:23] blay |
2013_2014:s2:td:corrections:td_classes-code [2014/02/15 23:05] blay [La classe Trajet] |
||
---|---|---|---|
Line 319: | Line 319: | ||
+ | ==== Gérer la vitesse : spécialisation de classes et enuméré ==== | ||
On ajoute la vitesse comme un enumerate dans le modèle et les méthodes demandées puis on met à jour les codes. | On ajoute la vitesse comme un enumerate dans le modèle et les méthodes demandées puis on met à jour les codes. | ||
Line 327: | Line 327: | ||
public enum Vitesse { | public enum Vitesse { | ||
arret, lent, moyen, rapide | arret, lent, moyen, rapide | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code java> | ||
+ | package outilsPK; | ||
+ | |||
+ | public class Tondeuse extends OutilElectrique { | ||
+ | |||
+ | private Vitesse vitesse ; | ||
+ | |||
+ | protected Vitesse getVitesse() { | ||
+ | return vitesse; | ||
+ | } | ||
+ | |||
+ | public void switchOn() { | ||
+ | setCadence(1000); | ||
+ | } | ||
+ | |||
+ | public Tondeuse() { | ||
+ | setCadence(0); | ||
+ | vitesse = Vitesse.arret; | ||
+ | } | ||
+ | |||
+ | public void setVitesse(Vitesse v){ | ||
+ | vitesse = v; | ||
+ | } | ||
+ | |||
+ | |||
+ | public void switchOff() { | ||
+ | super.switchOff(); | ||
+ | vitesse = Vitesse.arret; | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | <code java> | ||
+ | package outilsPK; | ||
+ | import static org.junit.Assert.*; | ||
+ | import org.junit.Before; | ||
+ | import org.junit.Test; | ||
+ | |||
+ | public class OutilElectriqueTest { | ||
+ | |||
+ | private TailleHaie th; | ||
+ | private Tondeuse td; | ||
+ | |||
+ | @Before | ||
+ | public void setUp() throws Exception { | ||
+ | th = new TailleHaie(); | ||
+ | td = new Tondeuse(); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testSwitchOnTailleHaie() { | ||
+ | th.switchOn(); | ||
+ | assertEquals(4500, | ||
+ | th.getCadence()); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testSwitchOnTondeuse() { | ||
+ | td.switchOn(); | ||
+ | assertEquals(1000, | ||
+ | td.getCadence()); | ||
+ | } | ||
+ | @Test | ||
+ | public void testVitesseTondeuse() { | ||
+ | td.switchOn(); | ||
+ | td.setVitesse(Vitesse.moyen); | ||
+ | assertEquals(Vitesse.moyen, | ||
+ | td.getVitesse()); | ||
+ | td.switchOff(); | ||
+ | assertEquals(Vitesse.arret, | ||
+ | td.getVitesse()); | ||
+ | } | ||
+ | |||
+ | |||
+ | @Test | ||
+ | public void testSwitchOff() { | ||
+ | th.switchOff();td.switchOff(); | ||
+ | assertEquals(0, | ||
+ | th.getCadence()); | ||
+ | assertEquals(0, | ||
+ | td.getCadence()); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testSwitchOff_On_Off() { | ||
+ | testSwitchOff(); | ||
+ | testSwitchOnTondeuse(); | ||
+ | testSwitchOnTailleHaie(); | ||
+ | testSwitchOff(); | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
Line 336: | Line 432: | ||
+ | ==== La classe Chemin ==== | ||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | |||
+ | import java.awt.*; | ||
+ | |||
+ | public class Chemin { | ||
+ | |||
+ | Point depart; | ||
+ | Point arrivee; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * @param depart | ||
+ | * @param arrivee | ||
+ | */ | ||
+ | public Chemin(Point depart, Point arrivee) { | ||
+ | this.depart = depart; | ||
+ | this.arrivee = arrivee; | ||
+ | } | ||
+ | public double distance() { | ||
+ | int y = arrivee.y - depart.y; | ||
+ | int x = arrivee.x - depart.x; | ||
+ | return Math.sqrt( y*y + x*x); | ||
+ | } | ||
+ | @Override | ||
+ | public String toString() { | ||
+ | return "Chemin [depart=" + depart + ", arrivee=" + arrivee + "]"; | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | |||
+ | import static org.junit.Assert.*; | ||
+ | |||
+ | import java.awt.Point; | ||
+ | |||
+ | import org.junit.Before; | ||
+ | import org.junit.Test; | ||
+ | |||
+ | |||
+ | public class CheminTest { | ||
+ | private static final double DELTA = 1e-15; | ||
+ | Chemin c ; | ||
+ | |||
+ | @Before | ||
+ | public void setUp() throws Exception { | ||
+ | c = new Chemin(new Point(-7,-2), new Point(5,3)); | ||
+ | } | ||
+ | |||
+ | |||
+ | @Test | ||
+ | public void testDistance() { | ||
+ | System.out.println(c.distance()); | ||
+ | assertEquals(13.0,c.distance(),DELTA); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </code> | ||
+ | ==== La classe Trajet ==== | ||
+ | |||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | public class MauvaisTrajetException extends Exception { | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | import java.awt.*; | ||
+ | |||
+ | public class Chemin { | ||
+ | Point depart; | ||
+ | Point arrivee; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * @param depart | ||
+ | * @param arrivee | ||
+ | */ | ||
+ | public Chemin(Point depart, Point arrivee) { | ||
+ | this.depart = depart; | ||
+ | this.arrivee = arrivee; | ||
+ | } | ||
+ | public double distance() { | ||
+ | int y = arrivee.y - depart.y; | ||
+ | int x = arrivee.x - depart.x; | ||
+ | return Math.sqrt( y*y + x*x); | ||
+ | } | ||
+ | @Override | ||
+ | public String toString() { | ||
+ | return "Chemin [depart=" + depart + ", arrivee=" + arrivee + "]"; | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | |||
+ | import java.util.*; | ||
+ | |||
+ | public class Trajet { | ||
+ | |||
+ | ArrayList<Chemin> chemins; | ||
+ | |||
+ | public Trajet(ArrayList<Chemin> chemins) throws MauvaisTrajetException { | ||
+ | super(); | ||
+ | this.chemins = chemins; | ||
+ | Iterator<Chemin> it = chemins.iterator(); | ||
+ | if (!(it.hasNext())) | ||
+ | throw new MauvaisTrajetException(); | ||
+ | Chemin courant = it.next(); | ||
+ | Chemin suivant; | ||
+ | while (it.hasNext()) { | ||
+ | suivant = it.next(); | ||
+ | if (courant.arrivee.equals(suivant.depart)) | ||
+ | courant = suivant; | ||
+ | else | ||
+ | throw new MauvaisTrajetException(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | return "Trajet [chemins=" + chemins + "]"; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code java> | ||
+ | package trajetPK; | ||
+ | |||
+ | import static org.junit.Assert.*; | ||
+ | |||
+ | import java.awt.Point; | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.Arrays; | ||
+ | |||
+ | import org.junit.Before; | ||
+ | import org.junit.Test; | ||
+ | |||
+ | public class TrajetTest { | ||
+ | |||
+ | private static final double DELTA = 1e-15; | ||
+ | Trajet trValide ; | ||
+ | Trajet trUn ; | ||
+ | Trajet trInvalide ; | ||
+ | Chemin c1 ; | ||
+ | Chemin c2 ; | ||
+ | Chemin c3; | ||
+ | @Before | ||
+ | public void setUp() throws Exception { | ||
+ | c1 = new Chemin(new Point(-7,-2), new Point(5,3) ); | ||
+ | c2 = new Chemin(new Point(5,3), new Point(7,5) ); | ||
+ | c3 = new Chemin(new Point(7,5) , new Point(9,11) ); | ||
+ | } | ||
+ | |||
+ | |||
+ | @Test | ||
+ | public void testCreerValide() throws MauvaisTrajetException { | ||
+ | trValide = new Trajet ( | ||
+ | new ArrayList<Chemin>(Arrays.asList(new Chemin[]{ c1, c2, c3 }))); | ||
+ | System.out.println(trValide); | ||
+ | } | ||
+ | @Test | ||
+ | public void testUn() throws MauvaisTrajetException { | ||
+ | trUn = new Trajet ( | ||
+ | new ArrayList<Chemin>(Arrays.asList(new Chemin[]{ c1}))); | ||
+ | } | ||
+ | @Test(expected = MauvaisTrajetException.class) | ||
+ | public void testInvalide() throws MauvaisTrajetException { | ||
+ | trInvalide = new Trajet ( | ||
+ | new ArrayList<Chemin>(Arrays.asList(new Chemin[]{ c1, c3 }))); | ||
+ | } | ||
+ | </code> | ||
==== La classe ''Polygone'' ==== | ==== La classe ''Polygone'' ==== | ||