Loading...
Loading...
A voter registration desk refuses impossible ages and turns away people under 18.
Write InvalidAgeException extends RuntimeException with a constructor that passes its message to super(message). Write a class Voter whose constructor throws it when the age is below 0 or above 120, and a method boolean canVote(). Catch the exception in main.
REGISTERED name, TOO YOUNG name, age a or REJECTED name: age a is not valid. Then REGISTERED r OF n.Example
Input
3 Asha 25 Ravi 16 Kiran -3
Output
REGISTERED Asha TOO YOUNG Ravi, age 16 REJECTED Kiran: age -3 is not valid REGISTERED 1 OF 3
Ravi is under 18; -3 is not a real age.
Try the problem first. Open one hint at a time.
The message is built where the exception is thrown: throw new InvalidAgeException("age " + age + " is not valid").
Put new Voter(...) inside try; when the constructor throws, no object is made.
An unchecked exception (from RuntimeException) needs no throws clause.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor