Answered step by step
Verified Expert Solution
Link Copied!

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

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Starting Out With Java From Control Structures Through Data Structures

Authors: Tony Gaddis

6th Edition

0133957055, 978-0133957051

More Books

Students also viewed these Programming questions