CJCoding With Joseph
15free / daySign inCreate account

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 the output shown in Expected Output.

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

Expected Output:

True
25

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Encapsulation
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.