2013_2014:s2:td:corrections:td_classes-code
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| 2013_2014:s2:td:corrections:td_classes-code [2014/02/15 20:30] – [Gérer la vitesse : spécialisation de classes et enuméré] blay | 2013_2014:s2:td:corrections:td_classes-code [2014/03/17 17:04] (current) – [La classe ''Polygone''] blay | ||
|---|---|---|---|
| Line 254: | Line 254: | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| package outilsPK; | package outilsPK; | ||
| Line 432: | Line 435: | ||
| - | ==== La classe '' | + | ==== La classe |
| + | <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 " | ||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <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, | ||
| + | } | ||
| + | |||
| + | |||
| + | @Test | ||
| + | public void testDistance() { | ||
| + | System.out.println(c.distance()); | ||
| + | assertEquals(13.0, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ==== La classe | ||
| + | |||
| + | <code java> | ||
| + | package trajetPK; | ||
| + | public class MauvaisTrajetException extends Exception { | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | package trajetPK; | ||
| + | |||
| + | import java.util.*; | ||
| + | |||
| + | public class Trajet { | ||
| + | |||
| + | ArrayList< | ||
| + | |||
| + | public Trajet(ArrayList< | ||
| + | super(); | ||
| + | this.chemins = 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 " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <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, | ||
| + | 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< | ||
| + | System.out.println(trValide); | ||
| + | } | ||
| + | @Test | ||
| + | public void testUn() throws MauvaisTrajetException { | ||
| + | trUn = new Trajet ( | ||
| + | new ArrayList< | ||
| + | } | ||
| + | @Test(expected = MauvaisTrajetException.class) | ||
| + | public void testInvalide() throws MauvaisTrajetException { | ||
| + | trInvalide = new Trajet ( | ||
| + | new ArrayList< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | {{ : | ||
| + | ==== La classe '' | ||
| - | Un polygone est composé d'un ensemble de points. | + | Un polygone est composé d'un ensemble de points. |
| <code java> | <code java> | ||
| Line 475: | Line 632: | ||
| ==== La classe '' | ==== La classe '' | ||
| - | * Pour | + | * Pour |
| - | point à ceux déjà | + | |
| - Etendez votre modélisation de la classe '' | - Etendez votre modélisation de la classe '' | ||
2013_2014/s2/td/corrections/td_classes-code.1392496251.txt.gz · Last modified: 2014/02/15 20:30 by blay