Task 6 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

4.4 Encapsulation — validating setter

Total: 0 / 4 pts
Practice mode — check each part and get hints as you go.
Task

Fill in setAge so that it stores the value only if it is 0 or more; a negative value is ignored and the field keeps its old value.

main tries -5 first, then 20, so the program prints 0 and then 20. This is why fields are private: the setter guards them.

Your solution