CJCoding With Joseph
15free / daySign inCreate account

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 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 — print one after the last line.

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