Write a program that asks the user to input 5 positive integers. Write a method that's...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
Charlotte (age 40) is a surviving spouse and provides all of the support of her four minor children (ages 4, 8, 11, and 14) who live with her. She also maintains the household in which her parents...
-
Ivanhoe Company incurred research and development costs of $ 9 0 0 0 0 to develop a patent, and legal fees of $ 3 0 0 0 0 to register the patent. The patent has a legal life of 2 0 years and a useful...
-
Write a program that asks the user to input a vector of integers of arbitrary length. The program then counts the number of elements, the number of positive elements, and the number of negative...
-
Find the lengths of the curves. The curve r = a sin 2 (/2), 0 , a > 0
-
The Glass Doctors repair chips in car windshields. The company incurred the following operating costs for the month of July 2018: Salaries and wages ............................................ $...
-
Lin Lowe plans to deposit $1,800 at the end of every 6 months for the next 15 years at 8% interest compounded semiannually. What is the value of Lins annuity at the end of 15 years?
-
For an endothermic reaction (a) \(\Delta H=+\mathrm{ve}\) (b) \(\Delta H=-\mathrm{ve}\) (c) \(\Delta H=\infty\) (d) \(\Delta H=0\).
-
The balance sheets of Petrello Company and Sanchez Company as of January 1, 2011, are presented below. On that date, after an extended period of negotiation, the two companies agreed to merge. To...
-
Danner Company expects to have a cash balance of $45,000 on January 1, 2022. Relevant monthly budget data for the first 2 months of 2022 are as follows. Collections from customers: January $85,000,...
-
Darden Restaurants, Inc. (DRI) is the largest full-service restaurant company in the world. It operates over 2,200 restaurants under a variety of brand names, including Olive Garden, Bahama Breeze,...
-
You were recently hired as a Virtualization Architect for Contoso Corporation. You have been highly recommended as being a creative and cost driven virtualization solution designer. Contoso has 1400...
-
The liquid phase activity coefficients of the ethanol(1) + toluene(2) system at 55C are given by the two-parameter Margules equation, where A 12 = 1.869 and A 21 = 1.654. (a) Show that the pure...
-
One of the things that is slowing the development of nanotechnology is the legal morass of patents that anyone working with new ideas must deal with. Some have argued that the government should give...
-
Author Charles Murray has argued that museums actually inhibit rather than foster the appreciation of art. He points out that the technology exists to make essentially perfect copies of any major...
-
As telecommunications improve, performers can reach larger and larger audiences. In the past, one could only perform in a concert hall; today one can perform for the entire world. How might that...
-
In the early 2000s, the wholesale price of the generic drug fluoxetine (the generic for Prozac) was $3.60 per 100. a. Given that the cost of dispensing this drug was about $5 to $10 per prescription,...
-
Portfolio Variance Outcomes Boom Normal Bust Probability(p) 0.2 0.5 0.3 rx 40% 26% -10% Wx= E(rp) = WE(r.) + W,E(r.) ry 50% 34% -20% E(r) = 18%, E(ry) = 21% Suppose the current market prices of...
-
An interest bearing promissory note for 90 days at 5.6% p.a. has a face value of $120,000. If the note is discounted 20 days after the issue date at a rate of 6.8% p.a., calculate the amount of...
-
Write a program that picks four cards from a deck of 52 cards and computes their sum. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number...
-
Assume a runner runs 24 miles in 1 hour, 40 minutes, and 35 seconds. Write a program that displays the average speed in kilometers per hour.
-
Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Use the % operator to extract...
-
True or false? Explain: hierarchy reduces complexity because A. It reduces the size of individual modules. B. It cuts down on the number of interconnections between elements. C. It assembles a number...
-
True or false? Explain: modularity reduces complexity because A. It reduces the effect of incommensurate scaling. B. It helps control propagation of effects.
-
Consider the part of the file system naming hierarchy illustrated in the following: a. In the path name and in the figure, identify the context that you should use for that resolution and the context...
Study smarter with the SolutionInn App