Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
Heres a Java program that fulfills your requirements by creating a custom exception class called Inv...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started