Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program that asks the user to input 5 positive integers. Write a method that's job is to read one positive value. Send
Write a program that asks the user to input 5 positive integers. Write a method that's job is to read one positive value. Send the Scanner from the main as an input parameter and then call nextInt() method from the new method. Have your method then return that positive number or throw exceptions (to the main) if given values are negative, or non-integer inputs. Make sure your main method catches each different type of exception and output an appropriate message to the screen telling the user went wrong. If the user fails on any of the five inputs they will need to input that one again. In other words, any time your method throws an exception it will be necessary to call it again. The user should not have to repeat all 5 again for one bad number. After 5 successful inputs report the average to the screen. You need to use a loop here, do not just write the same code out 5 times. IMPORTANT NOTE: Your method should throw, but not catch the exception. Exceptions are thrown to admit that we the programmer of the method do not know how to handle certain conditions. It almost never makes sense to throw and then catch an exception in the same block(method), because if we know what to write in the catch block we can do it without using exceptions.
Step by Step Solution
★★★★★
3.36 Rating (149 Votes )
There are 3 Steps involved in it
Step: 1
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