โ Back to Encapsulation
Question 465
Validate in a Setter
Validate in a Setter
Create a classStudentwith aprivate int age. WriteSetAge(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 aMainmethod. AfterSetAge(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.