CJCoding With Joseph

Validation Method Returning Bool

The User class has a private field age. Add SetAge(int value) that stores the value and returns true only when it is between 0 and 120 inclusive; otherwise it leaves age unchanged and returns false. Add GetAge() that returns the stored age. The tester prints the result of SetAge on one line, then the age on the next. Do NOT write a Main method.

For SetAge(25) the program prints:
True
25

For SetAge(-5) the program prints:
False
0

Expected Output:

True
25
Topics:
Encapsulation
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cw, cr, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.