Dans ce TD :
Trajet/Journey
comme une suite ordonnée de points.On décide de produire une nouvelle application telle que :
Evidemment vous commencez par identifier les cas d'utilisation de haut niveau. Mais ensuite vous pouvez travailler comme bon vous semble.
date=Thu Feb 28 21:39:16 CET 2019
) ; (2) point le plus proche quand il était le plus proche d'un point donné lors d'un parcours (e.g. (12,12) → date=Thu Feb 28 21:39:17 CET 2019
)Thu Feb 28 21:39:17 CET 2019
: le système renvoie (12,12) etc.Si vous voulez représenter la gestion du temps (mais vous pouvez vous en passer), voici une approche très simple :
try { Thread.sleep(10000);//1000 milliseconds is one second. } catch (InterruptedException e) { e.printStackTrace(); }
import java.util.Scanner; ..................... Scanner sc = new Scanner(System.in); int y = sc.nextInt(); boolean encore = sc.nextBoolean();
Attention, votre code ne correspond probablement pas à cela !!
Scanner sc = new Scanner(System.in); int interval = 1000; public Journey createJourney(Member m) throws InterruptedException{ System.out.println("Creating a journey "); DatedPoint currentPoint = enterDatedPoint(); ; Journey path = new Journey(currentPoint); boolean again = true; while (again){ currentPoint = enterDatedPoint(); path.addPoint(currentPoint); System.out.println("Distance : " + path.distance()); System.out.println("Duration since departure : " + path.duration() + " soit :" + path.duration()/1000 + " secondes" ); System.out.println("We continue ? (true or false)"); again = sc.nextBoolean(); Thread.sleep(interval); //1000 milliseconds is one second. } return path; } private DatedPoint enterDatedPoint() { DatedPoint currentPoint; int x; int y; System.out.println(" X "); x = sc.nextInt(); System.out.println(" Y "); y = sc.nextInt(); //obtenir la date courante Date date = new Date(); currentPoint = new DatedPoint(x,y,date); return currentPoint; }
Autres exemples
//To improve taking into account the displacement public Date whenWasYouHere(Point point) { DatedPoint closestPoint = points[0]; double distanceMin = point.distance(closestPoint); for (int i =1; i<nextPlace;i++) { double distance = point.distance(points[i]); if (distance < distanceMin) { distanceMin = distance; closestPoint = points[i]; } } return closestPoint.getDate(); } //To improve taking into account the displacement public DatedPoint whereWereYou(Date date) { long time = date.getTime(); DatedPoint closestPoint = points[0]; long durationMin = Math.abs(closestPoint.getDate().getTime()-time); for (int i =1; i<nextPlace;i++) { long duration = Math.abs(points[i].getDate().getTime()-time); if (duration < durationMin) { durationMin = duration; closestPoint = points[i]; } } return closestPoint; }
Et des exemples de tests …
@Test void testClosestPoint() throws InterruptedException { Date currentDate1 = new Date(); DatedPoint currentPoint1 = new DatedPoint(5,5,currentDate1) ; Thread.sleep(1000); Date currentDate2 = new Date(); DatedPoint currentPoint2 = new DatedPoint(7,7,currentDate2) ; Thread.sleep(1000); DatedPoint currentPoint3 = new DatedPoint(10,10,new Date()) ; Thread.sleep(1000); Date currentDate3 = new Date(); Journey path = new Journey(currentPoint1); path.addPoint(currentPoint2); path.addPoint(currentPoint3); assertEquals(currentPoint1.getDate(), path.whenWasYouHere(currentPoint1)); assertEquals(currentPoint2.getDate(), path.whenWasYouHere(currentPoint2)); assertEquals(currentPoint3.getDate(), path.whenWasYouHere(currentPoint3)); assertEquals(currentPoint3.getDate(), path.whenWasYouHere(new DatedPoint(12,12,new Date()))); assertEquals(currentPoint1.getDate(), path.whenWasYouHere(new DatedPoint(4,4,new Date()))); assertEquals(currentPoint2.getDate(), path.whenWasYouHere(new DatedPoint(8,8,new Date()))); } @Test void testWhereWereYou() throws InterruptedException { Date currentDate1 = new Date(); DatedPoint currentPoint1 = new DatedPoint(5,5,currentDate1) ; Thread.sleep(1000); Date currentDate2 = new Date(); DatedPoint currentPoint2 = new DatedPoint(7,7,currentDate2) ; Thread.sleep(1000); DatedPoint currentPoint3 = new DatedPoint(10,10,new Date()) ; Thread.sleep(1000); Date currentDate3 = new Date(); Journey path = new Journey(currentPoint1); path.addPoint(currentPoint2); path.addPoint(currentPoint3); assertEquals(currentPoint1, path.whereWereYou(currentDate1)); assertEquals(currentPoint2, path.whereWereYou(currentDate2)); assertEquals(currentPoint3, path.whereWereYou(currentDate3)); assertEquals(currentPoint3, path.whereWereYou(new Date())); assertEquals(currentPoint1, path.whereWereYou(new Date(currentDate1.getTime()-1000)) ); }
Erreurs