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:25] blay [Gérer la vitesse : spécialisation de classes et enuméré] |
2013_2014:s2:td:corrections:td_classes-code [2014/02/15 23:12] blay [La classe Trajet] |
||
---|---|---|---|
Line 254: | Line 254: | ||
} | } | ||
+ | </code> | ||
+ | |||
+ | <code java> | ||
package outilsPK; | package outilsPK; | ||
Line 361: | Line 364: | ||
+ | } | ||
+ | </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 370: | Line 435: | ||
+ | ==== 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.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> | ||
+ | |||
+ | |||
+ | |||
+ | {{ :2013_2014:s2:td:corrections:trajet.png?nolink&300 |}} | ||
==== La classe ''Polygone'' ==== | ==== La classe ''Polygone'' ==== | ||