Question: Write a Java program that prompts the user to enter their name and age, validates the inputs, and prints a greeting message to the

Write a Java program that prompts the user to enter their name and age, validates the inputs, and prints a

Write a Java program that prompts the user to enter their name and age, validates the inputs, and prints a greeting message to the console. If the user enters an invalid age, the program should throw a custom exception called "Invalid InputException". The program should be implemented as follows: I. Create a custom exception class called "Invalid InputException that printout the error message "Invalid age: Age should be a positive integer less than or equal to 120" if the age is a negative number or greater than 120. II. Create another class "GreetingValidator" to get the user inputs through the main method. The program should prompt the user to enter their name and age. III. The program should validate the age by checking that it is a positive integer less than or equal to 120. If the age is invalid, the program should throw an Invalid InputException. If both the age is valid, the program should print a greeting message to the console that includes the user's name and age. IV. Sample Output 1: Enter your name: Ihilini Enter your age: 150 Invalid age: Age should be a positive integer less than or equal to 120. Sample Output 2: Enter your name: Thilini Enter your age: 30 Hello Thilini, you are 30 years old.

Step by Step Solution

3.34 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres a Java program that fulfills your requirements by creating a custom exception class called Inv... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!