CJCoding With Joseph

Validate in a Setter

Create a class Student with a private int age. Write SetAge(int value) so it stores the value only when it is 0 or greater; a negative value is ignored and the age keeps its previous value. GetAge() returns the current age. Do not write a Main method.

After SetAge(20) the program prints:
20

On a brand-new student (age still 0), SetAge(-5) is ignored and the program prints:
0

Expected Output:

20
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.