Answered step by step
Verified Expert Solution
Link Copied!

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 non-integer 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 exception-handling techniques to ensure that each entered array value is a double.
Use the sentinel value 99999 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 >>3
Enter a numeric value or 99999 to quit >>1
Enter next numeric value or 99999 to quit >>2
Enter next numeric value or 99999 to quit >>3
You entered 3 numbers and their average is 2.0
1.0 is 1.0 away from the average
2.0 is 0.0 away from the average
3.0 is -1.0 away from the average
Task 01: Create the DistanceFromAverageWithExceptionHandling class.
Task 02: 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 03: The DistanceFromAverageWithExceptionHandling catches any NegativeArraySizeException thrown and defaults to an array size of 5.
Test Feedback:
Status: FAILED!
Test: The `DistanceFromAverageWithExceptionHandling` program catches any `NegativeArraySizeException` thrown.
Reason: In this simulation, the input values were 19,5,11,15, and 1.19 is -8.8 away from the average. Unable to find '-8.8' 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 26,27,1,18, and 12.26 is -9.2 away from the average. Unable to find '-9.2' in the program's output.
Error : class java.lang.AssertionError
----------
Status: FAILED!
Test: The `DistanceFromAverageWithExceptionHandling` program ends the input with sentinel value 99999.
Reason: In this simulation, the input values were 1,3.3 is -1 away from the average. Unable to find '-1' in the program's output.
Error : class java.lang.AssertionError
----------
Your current grade is: 33.33%
import java.util.Scanner;
public class BonusBug10{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array >>");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter "+ size +" integers >>");
for (int i =0; i < size; i++){
array[i]= scanner.nextInt();
}
System.out.print("Enter an index to retrieve >>");
int index = scanner.nextInt();
try {
int value = array[index];
System.out.println("Value at index "+ index +" is: "+ value);
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array index out of bounds error: "+ e.getMessage());
}
scanner.close();
}
}

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

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

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions