Loading...
Loading...
A coaching centre admits students aged 5 to 25 only, and a name must have at least 2 letters. Each form is checked when the Student object is created.
Write a class Student with private name and age, getters, and setters that throw an IllegalArgumentException with a clear message. The constructor must call the setters, so a Student with bad data can never be created. Check the name first, then the age.
ADMITTED name age, or REJECTED name: message, where the message is name must have at least 2 letters or age must be between 5 and 25. Then a last line ADMITTED k OF n.Example
Input
3 Asha 17 Ravi 30 K 10
Output
ADMITTED Asha 17 REJECTED Ravi: age must be between 5 and 25 REJECTED K: name must have at least 2 letters ADMITTED 1 OF 3
Ravi is too old and K's name is too short.
Try the problem first. Open one hint at a time.
A setter that throws stops the constructor at once, so the half-built object is never handed back.
The constructor calls setName and setAge, so the rules are written once.
In main, put new Student(...) inside try and print e.getMessage() in the catch.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor