Question

In Chapter 8, you wrote an application named DistanceFromAverage that allows a user to enter up...

In Chapter 8, you wrote an application named DistanceFromAverage that allows a user to enter up to 15 double values and then displays each entered value and its distance from the average. Now, modify that program to first prompt the user to enter an integer that represents the array size. Java generates a NumberFormatException if you attempt to enter a noninteger value using nextInt(); handle this exception by displaying an appropriate error message.

Create an array 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 before the program calculates each element’s distance from the average.

===================================================================

import java.util.*;
public class DistanceFromAverageWithExceptionHandling
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
double[] numbers = new double[15];
double entry;
double total = 0;
double average = 0;
final int QUIT = 99999;
int x = 0, y;
System.out.print("Enter a numeric value or " +
QUIT + " to quit >> ");
entry = input.nextDouble();
while(entry != QUIT && x < numbers.length)
{
numbers[x] = entry;
total += numbers[x];
++x;
if(x < numbers.length)
{
System.out.print("Enter next numeric value or " +
QUIT + " to quit >> ");
entry = input.nextDouble();
}
}
if(x == 0)
System.out.println("Average cannot be computed because no numbers were entered");
else
{
average = total / x;
System.out.println("You entered " + x +
" numbers and their average is " + average);
for(y = 0; y < x; ++y)
System.out.println(numbers[y] + " is " +
(numbers[y] - average) + " away from the average");
}   
}
}

Homework Answers

Answer #1

If you need any corrections/clarifications kindly comment

Program

import java.util.*;
public class DistanceFromAverageWithExceptionHandling
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String val;
double entry=0;
double total = 0;
double average = 0;
final int QUIT = 99999;
int x = 0, y,size=0,count=0;
double[] numbers = new double[5];
boolean isValOk = false;
boolean success = false;
while (!success) {
try {
System.out.print("Enter array size: ");
val=input.next();
size=Integer.parseInt(val);
success = true;
} catch (NumberFormatException e) {
System.out.println("You have entered invalid data");
}
}


try {
numbers = new double[size];

} catch (NegativeArraySizeException ex) {
System.out.println("Can't create array of negative size!!!So setting the array size to a default value of five.");
size=5;
}

while(entry != QUIT && x < numbers.length)
{

if(x < numbers.length)
{
try
{
System.out.print("Enter next numeric value or " +QUIT + " to quit >> ");
entry = input.nextDouble();
numbers[x] = entry;
total += numbers[x];
++x;
}
catch(InputMismatchException e)
{

System.out.println("Entered array value should be a double");
input.nextLine();
}
}
}
if(x == 0)
System.out.println("Average cannot be computed because no numbers were entered");
else
{
average = total / x;
System.out.println("You entered " + x +
" numbers and their average is " + average);
for(y = 0; y < x; ++y)
System.out.println(numbers[y] + " is " +
(numbers[y] - average) + " away from the average");
}
}
}

Output

Enter array size: -2
Can't create array of negative size!!!So setting the array size to a default value of five.
Enter next 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
Enter next numeric value or 99999 to quit >> word
Entered array value should be a double
Enter next numeric value or 99999 to quit >> 4
Enter next numeric value or 99999 to quit >> 5
You entered 5 numbers and their average is 3.0
1.0 is -2.0 away from the average
2.0 is -1.0 away from the average
3.0 is 0.0 away from the average
4.0 is 1.0 away from the average
5.0 is 2.0 away from the average

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
."Ask the user to input a number.  You must use an input dialog box for this input....
."Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user if they want to enter another number. If yes, repeat the process. If no, output the smallest and largest number that...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
The following program allows the user to enter the grades of 10 students in a class...
The following program allows the user to enter the grades of 10 students in a class in an array called grade. In a separate loop, you need to test if a grade is passing or failing, and copy the grade to an array to store passing or failing grades accordingly. A passing grade is a grade greater than or equal to 60. You can assume the use will enter a grade in the range of 0 to 100, inclusive. ...
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Complete Programming Assignment Chapter 10. Use program In JAVA ProductCodes.java on pages 446-447, it will provide...
Complete Programming Assignment Chapter 10. Use program In JAVA ProductCodes.java on pages 446-447, it will provide you most of the needed code. You are to design and implement a program that reads a series of 10 integers from user and print the average. Read each integer as a string and convert to integer using Integer.parseInt method. If this process throws a NumberFormatException (not a valid number), catch and handle the exception by printing an error message "Not a valid entry,...