|
| 1 | +In this exercise you'll be organizing races between various types of remote controlled cars. Each car has its own speed and battery drain characteristics. |
| 2 | + |
| 3 | +Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers the car's speed in meters and decreases the remaining battery percentage by its battery drain. |
| 4 | + |
| 5 | +If a car's battery is below its battery drain percentage, you can't drive the car anymore. |
| 6 | + |
| 7 | +Each race track has its own distance. Cars are tested by checking if they can finish the track without running out of battery. |
| 8 | + |
| 9 | +You have six tasks, each of which will work with remote controller car instances. |
| 10 | + |
| 11 | +## 1. Creating a remote controlled car |
| 12 | + |
| 13 | +Allow creating a remote controller car by defining a constructor for the `RemoteControlCar` class that takes the speed of the car in meters and the battery drain percentage as its two parameters (both of type `int`): |
| 14 | + |
| 15 | +```java |
| 16 | +int speed = 5; |
| 17 | +int batteryDrain = 2; |
| 18 | +var car = new RemoteControlCar(speed, batteryDrain); |
| 19 | +``` |
| 20 | + |
| 21 | +## 2. Creating a race track |
| 22 | + |
| 23 | +Allow creating a race track by defining a constructor for the `RaceTrack` class that takes the track's distance in meters as its sole parameter (which is of type `int`): |
| 24 | + |
| 25 | +```java |
| 26 | +int distance = 800; |
| 27 | +var raceTrack = new RaceTrack(distance); |
| 28 | +``` |
| 29 | + |
| 30 | +## 3. Drive the car |
| 31 | + |
| 32 | +Implement the `RemoteControlCar.drive()` method that updates the number of meters driven based on the car's speed. Also implement the `RemoteControlCar.distanceDriven()` method to return the number of meters driven by the car: |
| 33 | + |
| 34 | +```java |
| 35 | +int speed = 5; |
| 36 | +int batteryDrain = 2; |
| 37 | +var car = new RemoteControlCar(speed, batteryDrain); |
| 38 | +car.drive(); |
| 39 | + |
| 40 | +car.distanceDriven(); |
| 41 | +// => 5 |
| 42 | +``` |
| 43 | + |
| 44 | +## 4. Check for a drained battery |
| 45 | + |
| 46 | +Update the `RemoteControlCar.drive()` method to drain the battery based on the car's battery drain. Also implement the `RemoteControlCar.batteryDrained()` method that indicates if the battery is drained: |
| 47 | + |
| 48 | +```java |
| 49 | +int speed = 5; |
| 50 | +int batteryDrain = 2; |
| 51 | +var car = new RemoteControlCar(speed, batteryDrain); |
| 52 | +car.drive(); |
| 53 | + |
| 54 | +car.batteryDrained(); |
| 55 | +// => false |
| 56 | +``` |
| 57 | + |
| 58 | +## 5. Create the Nitro remote control car |
| 59 | + |
| 60 | +The best-selling remote control car is the Nitro, which has a stunning top speed of 50 meters with a battery drain of 4%. Implement the (static) `RemoteControlCar.nitro()` method to return this type of car: |
| 61 | + |
| 62 | +```java |
| 63 | +var car = RemoteControlCar.nitro(); |
| 64 | +car.drive(); |
| 65 | +car.distanceDriven(); |
| 66 | +// => 50 |
| 67 | +``` |
| 68 | + |
| 69 | +## 6. Check if a remote control car can finish a race |
| 70 | + |
| 71 | +To finish a race, a car has to be able to drive the race's distance. This means not draining its battery before having crossed the finish line. Implement the `Race.carCanFinish()` method that takes a `RemoteControlCar` instance as its parameter and returns `true` if the car can finish the race; otherwise, return `false`: |
| 72 | + |
| 73 | +```java |
| 74 | +int speed = 5; |
| 75 | +int batteryDrain = 2; |
| 76 | +var car = new RemoteControlCar(speed, batteryDrain); |
| 77 | + |
| 78 | +int distance = 100; |
| 79 | +var race = new Race(distance); |
| 80 | + |
| 81 | +race.carCanFinish(car); |
| 82 | +// => true |
| 83 | +``` |
0 commit comments