User Tools

Site Tools


2013_2014:s2:td:corrections:td_classes-code

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous 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/03/17 18:04] (current)
blay [La classe ''Polygone'']
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 ''​Polygone''​ ====+==== 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&​500 |}} 
 +==== La classe ''​Polygone'' ​V1 et V2 ====
  
-Un polygone est composé d'un ensemble de points.+Un polygone est composé d'un ensemble de points. ​J'ai utilisé de suite une liste de points qui permet de supporter le addPoint. Mais si j'​avais utilisé un tableau, alors j'​aurais modifié ma classe dans l'​exercice suivant pour supporter le addPoint. C'est trop tard ;-)
  
 <code java> <code java>
Line 413: Line 632:
  
 ==== La classe ''​Polygone'': ​ V2 ==== ==== La classe ''​Polygone'': ​ V2 ====
-  * Pour   ​remplir ​ le   ​tableau ​ de  points, ​ on  a besoin d'​une ​ première ​ méthode ​ ''​addPoint'' ​ qui  ajoutera ​ un   +  * Pour   ​remplir ​ le   ​tableau ​ de  points, ​ on  a besoin d'​une ​ première ​ méthode ​ ''​addPoint'' ​ qui  ajoutera ​ un   ​point  à  ceux  déjà ​ présents.  ​
-point  à  ceux  déjà ​ présents.  ​+
  
   - Etendez votre modélisation de la classe ''​Polygone''​ en UML.    - Etendez votre modélisation de la classe ''​Polygone''​ en UML. 
2013_2014/s2/td/corrections/td_classes-code.1392495931.txt.gz · Last modified: 2014/02/15 21:25 by blay