Validate in a Setter
mediumQuestion 555Create 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 the output shown in Expected Output.
On a brand-new student (age still 0), SetAge(-5) is ignored and the program prints:
0
Expected output
20= newline. The output ends with a newline, so print one after the last line.
Hints2 available
Sign in to use hintsTopics
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.