Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program called DistanceFromAverageWithExceptionHandling that prompts the user to enter an integer that represents an array size. Java generates a NumberFormatException if the user
Write a program called DistanceFromAverageWithExceptionHandling that prompts the user to enter an integer that represents an array size.
Java generates a NumberFormatException if the user attempts to enter a noninteger value using nextInt Handle this exception by displaying the following error message: "Invalid value for array size". If the size entered is an integer, create an array of type double using the integer entered as the size.
Java generates a NegativeArraySizeException if you attempt to create an array with a negative size; handle this exception by setting the array size to a default value of five. If the array is created successfully, use exceptionhandling techniques to ensure that each entered array value is a double.
Use the sentinel value to stop prompting the user for input values. Then calculate each elements distance from the average.
Examples of the program are shown below:
Please enter a value for the array size Mike
Invalid value for array size
Please enter a value for the array size
Enter a numeric value or to quit
Enter next numeric value or to quit
Enter next numeric value or to quit
You entered numbers and their average is
is away from the average
is away from the average
is away from the average
Task : Create the DistanceFromAverageWithExceptionHandling class.
Task : The DistanceFromAverageWithExceptionHandling catches any NumberFormatExceptions thrown.
Test Feedback:
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NumberFormatException thrown.
Reason: The program did not accept the simulated input: Viet null
Error : class java.util.NoSuchElementException
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NumberFormatException thrown.
Reason: java.util.NoSuchElementException
Error : class java.lang.AssertionError
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NumberFormatException thrown.
Reason: The program did not accept the simulated input: Lan null
Error : class java.util.NoSuchElementException
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NumberFormatException thrown.
Reason: java.util.NoSuchElementException
Error : class java.lang.AssertionError
Task : The DistanceFromAverageWithExceptionHandling catches any NegativeArraySizeException thrown and defaults to an array size of
Test Feedback:
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NegativeArraySizeException thrown.
Reason: In this simulation, the input values were and is away from the average. Unable to find in the program's output.
Error : class java.lang.AssertionError
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program catches any NegativeArraySizeException thrown.
Reason: In this simulation, the input values were and is away from the average. Unable to find in the program's output.
Error : class java.lang.AssertionError
Status: FAILED!
Test: The DistanceFromAverageWithExceptionHandling program ends the input with sentinel value
Reason: In this simulation, the input values were is away from the average. Unable to find in the program's output.
Error : class java.lang.AssertionError
Your current grade is:
import java.util.Scanner;
public class BonusBug
public static void mainString args
Scanner scanner new ScannerSystemin;
System.out.printEnter the size of the array ;
int size scanner.nextInt;
int array new intsize;
System.out.printlnEnter size integers ;
for int i ; i size; i
arrayi scanner.nextInt;
System.out.printEnter an index to retrieve ;
int index scanner.nextInt;
try
int value arrayindex;
System.out.printlnValue at index index is: value;
catch ArrayIndexOutOfBoundsException e
System.out.printlnArray index out of bounds error: egetMessage;
scanner.close;
Step by Step Solution
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