|
1 | | -# Instructions |
2 | | - |
3 | | -In this exercise you'll be playing around with a remote controlled car, which you've finally saved enough money for to buy. |
4 | | - |
5 | | -Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers 20 meters and drains one percent of the battery. |
6 | | - |
7 | | -The remote controlled car has a fancy LED display that shows two bits of information: |
8 | | - |
9 | | -- The total distance it has driven, displayed as: `"Driven <METERS> meters"`. |
10 | | -- The remaining battery charge, displayed as: `"Battery at <PERCENTAGE>%"`. |
11 | | - |
12 | | -If the battery is at 0%, you can't drive the car anymore and the battery display will show `"Battery empty"`. |
13 | | - |
14 | | -You have six tasks, each of which will work with remote controlled car instances. |
15 | | - |
16 | | -## 1. Buy a brand-new remote controlled car |
17 | | - |
18 | | -Implement the (_static_) `JedliksToyCar.buy()` method to return a brand-new remote controlled car instance: |
19 | | - |
20 | | -```java |
21 | | -JedliksToyCar car = JedliksToyCar.buy(); |
22 | | -``` |
23 | | - |
24 | | -## 2. Display the distance driven |
25 | | - |
26 | | -Implement the `JedliksToyCar.distanceDisplay()` method to return the distance as displayed on the LED display: |
27 | | - |
28 | | -```java |
29 | | -JedliksToyCar car = JedliksToyCar.buy(); |
30 | | -car.distanceDisplay(); |
31 | | -// => "Driven 0 meters" |
32 | | -``` |
33 | | - |
34 | | -## 3. Display the battery percentage |
35 | | - |
36 | | -Implement the `JedliksToyCar.batteryDisplay()` method to return the battery percentage as displayed on the LED display: |
37 | | - |
38 | | -```java |
39 | | -JedliksToyCar car = JedliksToyCar.buy(); |
40 | | -car.batteryDisplay(); |
41 | | -// => "Battery at 100%" |
42 | | -``` |
43 | | - |
44 | | -## 4. Update the number of meters driven when driving |
45 | | - |
46 | | -Implement the `JedliksToyCar.drive()` method that updates the number of meters driven: |
47 | | - |
48 | | -```java |
49 | | -JedliksToyCar car = JedliksToyCar.buy(); |
50 | | -car.drive(); |
51 | | -car.drive(); |
52 | | -car.distanceDisplay(); |
53 | | -// => "Driven 40 meters" |
54 | | -``` |
55 | | - |
56 | | -## 5. Update the battery percentage when driving |
57 | | - |
58 | | -Update the `JedliksToyCar.drive()` method to update the battery percentage: |
59 | | - |
60 | | -```java |
61 | | -JedliksToyCar car = JedliksToyCar.buy(); |
62 | | -car.drive(); |
63 | | -car.drive(); |
64 | | -car.batteryDisplay(); |
65 | | -// => "Battery at 98%" |
66 | | -``` |
67 | | - |
68 | | -## 6. Prevent driving when the battery is drained |
69 | | - |
70 | | -Update the `JedliksToyCar.drive()` method to not increase the distance driven nor decrease the battery percentage when the battery is drained (at 0%): |
71 | | - |
72 | | -```java |
73 | | -JedliksToyCar car = JedliksToyCar.buy(); |
74 | | - |
75 | | -// Drain the battery |
76 | | -// ... |
77 | | - |
78 | | -car.distanceDisplay(); |
79 | | -// => "Driven 2000 meters" |
80 | | - |
81 | | -car.batteryDisplay(); |
82 | | -// => "Battery empty" |
83 | | -``` |
| 1 | +# Instructions |
| 2 | + |
| 3 | +In this exercise you'll be playing around with a remote controlled car, which you've finally saved enough money for to buy. |
| 4 | + |
| 5 | +Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers 20 meters and drains one percent of the battery. |
| 6 | + |
| 7 | +The remote controlled car has a fancy LED display that shows two bits of information: |
| 8 | + |
| 9 | +- The total distance it has driven, displayed as: `"Driven <METERS> meters"`. |
| 10 | +- The remaining battery charge, displayed as: `"Battery at <PERCENTAGE>%"`. |
| 11 | + |
| 12 | +If the battery is at 0%, you can't drive the car anymore and the battery display will show `"Battery empty"`. |
| 13 | + |
| 14 | +You have six tasks, each of which will work with remote controlled car instances. |
| 15 | + |
| 16 | +## 1. Buy a brand-new remote controlled car |
| 17 | + |
| 18 | +Implement the (_static_) `JedliksToyCar.buy()` method to return a brand-new remote controlled car instance: |
| 19 | + |
| 20 | +```java |
| 21 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 22 | +``` |
| 23 | + |
| 24 | +## 2. Display the distance driven |
| 25 | + |
| 26 | +Implement the `JedliksToyCar.distanceDisplay()` method to return the distance as displayed on the LED display: |
| 27 | + |
| 28 | +```java |
| 29 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 30 | +car.distanceDisplay(); |
| 31 | +// => "Driven 0 meters" |
| 32 | +``` |
| 33 | + |
| 34 | +## 3. Display the battery percentage |
| 35 | + |
| 36 | +Implement the `JedliksToyCar.batteryDisplay()` method to return the battery percentage as displayed on the LED display: |
| 37 | + |
| 38 | +```java |
| 39 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 40 | +car.batteryDisplay(); |
| 41 | +// => "Battery at 100%" |
| 42 | +``` |
| 43 | + |
| 44 | +## 4. Update the number of meters driven when driving |
| 45 | + |
| 46 | +Implement the `JedliksToyCar.drive()` method that updates the number of meters driven: |
| 47 | + |
| 48 | +```java |
| 49 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 50 | +car.drive(); |
| 51 | +car.drive(); |
| 52 | +car.distanceDisplay(); |
| 53 | +// => "Driven 40 meters" |
| 54 | +``` |
| 55 | + |
| 56 | +## 5. Update the battery percentage when driving |
| 57 | + |
| 58 | +Update the `JedliksToyCar.drive()` method to update the battery percentage: |
| 59 | + |
| 60 | +```java |
| 61 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 62 | +car.drive(); |
| 63 | +car.drive(); |
| 64 | +car.batteryDisplay(); |
| 65 | +// => "Battery at 98%" |
| 66 | +``` |
| 67 | + |
| 68 | +## 6. Prevent driving when the battery is drained |
| 69 | + |
| 70 | +Update the `JedliksToyCar.drive()` method to not increase the distance driven nor decrease the battery percentage when the battery is drained (at 0%): |
| 71 | + |
| 72 | +```java |
| 73 | +JedliksToyCar car = JedliksToyCar.buy(); |
| 74 | + |
| 75 | +// Drain the battery |
| 76 | +// ... |
| 77 | + |
| 78 | +car.distanceDisplay(); |
| 79 | +// => "Driven 2000 meters" |
| 80 | + |
| 81 | +car.batteryDisplay(); |
| 82 | +// => "Battery empty" |
| 83 | +``` |
0 commit comments