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:30]
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 432: 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 475: 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.1392496251.txt.gz · Last modified: 2014/02/15 21:30 by blay