Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program called DistanceFromAverage that allows a user to enter up to 15 double values and then displays each entered value and its

image text in transcribedimage text in transcribed

Write a Java program called DistanceFromAverage that allows a user to enter up to 15 double values and then displays each entered value and its distance from the average. The program will first prompt the user for the size of the array (that is the number of values that will be displayed) followed by a prompt for each of the double values to put in the array. The program then calculates and prints the average of the values, and displays each value and its distance from the average. (format all output to one decimal place) This program should handle the following exceptions: 1. prompt for the size of the array- the Scanner class's nextInt(method generates a InputMismatchException (built in Java exception, so you don't need to create this one, just catch it. It's in java.util) if the user attempts to enter a non-integer value (that is, a number with decimal places, or a mixture of numbers and characters. Test for both) a. note the InputMismatchExceptiondefault constructor causes a null value to print, when you use the getMessage method (show that happens for your program) b. also tell the user that they didn't enter an integer, and to try again 2. creating the array (negative size), Java generates a NegativeArray Size Exception (another built-in Java exception, in java.lang so no need to import anything) if you attempt to create an array with a negative size a. handle this exception by displaying the error message from the object (by using the getMessage method) + b. set the array to a default size of 5 and let the user know you did this 3. creating the array (size too big) - create your own ArraySizeTooBig exception class if the user enters an array size bigger than 15- a. when you create the exception object include in the description the input value that is causing the problem- b. handle this exception by displaying the error message from the object (by using the getMessage method) C. set the array to a default size of 15and let the user know you did this 4. reading data into the array - use exception handling to ensure that each entered array value is a double (nextDouble(behaves the same as nextInt(). see #1 above) a. handle this exception by displaying the error message from the object (by using + t the getMessage method - this will display null) b. tell the user they didn't enter a proper double value. Ask the user to try again- c. use an infinite loop that stops when enough correct values have been read in- You will need to run the program multiple times to test exceptions 1-3, so you can cut and paste each exception message into a Word or Notepad document, then add the output from exception 4. + Note: you may need to do this at the beginning of your program to keep the compiler happy:- doublel] array = {0};+ Assignment submission: Hand in a copy of the program, and the output produced- Testing that your program works correctly:- you should have test cases for every circumstance that could go wrong- can you identify what tests you should use? (try to work these out before looking at the test cases below) Test cases: For exception #1+ O user enters a number with decimal places, eg 123.456+ o user enters something with non-numeric characters For exception #24 o user enters a negative integer For exception #3+ o user enters an integer greater than 15 For exception #4 o user enters something with non-numeric characters

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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_2

Step: 3

blur-text-image_3

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions