Created
April 21, 2011 09:34
-
-
Save davidjmemmett/934067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Vehicle { | |
private $speed; | |
private $distance_travelled; | |
private $time_taken; | |
public function setSpeed($speed) { | |
$this->speed = $speed; | |
} | |
public function travelTime($time) { | |
$this->time_taken += $time; | |
$this->distance_travelled += $this->getDistance($this->speed, $time); | |
} | |
public function isLevelWith(Vehicle $v) { | |
return $v->getDistanceTravelled() == $this->getDistanceTravelled(); | |
} | |
private function getDistanceTravelled() { | |
return $this->distance_travelled; | |
} | |
public function dump() { | |
printf("speed: %ukph\n", $this->speed); | |
printf("distance_travelled: %ukm\n", $this->distance_travelled); | |
printf("time_taken: %uh\n", $this->time_taken); | |
} | |
private static function getDistance($speed, $time) { | |
return $speed * $time; | |
} | |
} | |
$v1 = new Vehicle(); | |
$v1->setSpeed(60); | |
$v1->travelTime(7/60); | |
$v2 = new Vehicle(); | |
$v2->setSpeed(95); | |
while (!$v1->isLevelWith($v2)) { | |
printf("v1:\n"); | |
$v1->travelTime(1/60); | |
$v1->dump(); | |
printf("\nv2:\n"); | |
$v2->travelTime(1/60); | |
$v2->dump(); | |
printf("\n--\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment