1
1
Fork 0
AtomicKotlinCourse/Preventing Failure/Check Instructions/Exercise 3/task.md

24 lines
1.0 KiB
Markdown
Raw Normal View History

## Check Instructions (#3)
This exercise further explores ranges, introduced in [Looping &
2020-11-18 20:35:46 +03:00
Ranges](https://stepik.org/lesson/104311/step/1), and shows how they can be used with check
instructions.
Create a class `Level` with two constructor arguments: `val range: IntRange`,
2020-12-12 02:30:51 +03:00
and `private var level: Int`. `level` has a default argument that is the bottom
value of `range`. The constructor should ensure that `level` is one of the
values in `range`.
Add a read-only `value` property that produces the value of `level`.
Add a function `up()` that increments `level` only if the result is within
2020-12-12 02:30:51 +03:00
`range`, and a function `down()` that decrements `level` only if the result is
within `range`. Finally, add a function `set(new: Int)` that first ensures that
`new` is within `range` and then assigns `new` to `level`.
`Level` is tested by the starter code in `main()`.
<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>