2020-09-28 18:08:41 +03:00
|
|
|
## Late Initialization (#3)
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
The starter code provides:
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
```kotlin
|
|
|
|
data class Generator(val id: Int)
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
class Turbine(val id: Int) {
|
|
|
|
private lateinit var _generator: Generator
|
|
|
|
val generator: Generator
|
|
|
|
...
|
|
|
|
```
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
Add a `get()` for `generator` that checks to see if `_generator` has been
|
|
|
|
initialized, and if not initializes it before returning `_generator`. Add a
|
|
|
|
`toString()` that starts with "Generator $id running: " and then indicates
|
|
|
|
if the generator is running.
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
The starter code provides:
|
2020-09-27 01:27:15 +03:00
|
|
|
|
2020-09-28 18:08:41 +03:00
|
|
|
```kotlin
|
|
|
|
class PowerPlant(nTurbines: Int = 4) {
|
|
|
|
private val turbines: List<Turbine> =
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
Finish the initialization for `turbines`, then add
|
|
|
|
`fun generator(number: Int): Generator` which checks to make sure `number`
|
|
|
|
is in range, then returns the desired generator. Finally, add a `status()`
|
|
|
|
member function that uses `forEach` to show the status of each generator in the
|
2020-10-12 13:11:53 +03:00
|
|
|
`PowerPlant`. The code in `main()` tests your solution.
|
2020-12-13 01:54:57 +03:00
|
|
|
|
|
|
|
<sub> This task doesn't contain automatic tests,
|
|
|
|
so it's always marked as "Correct" when you run "Check".
|
|
|
|
Please compare your solution with the one provided! </sub>
|