For this project, you will write a Java program that demonstrates exception hand
ID: 3863883 • Letter: F
Question
For this project, you will write a Java program that demonstrates exception handling. Your program will accept from the user ten values and place those numbers in an array. The numbers in the array will be added together and the result displayed to the user. The program will also perform division. The program should compare the values for elements 1 and 2 in the array and divide the larger number by the smaller number. It should compare the values for all odd/even elements and divide the larger by the smaller value. The program should use exception handling to avoid division by zero errors or out of range errors (dictated by the variable’s data type) which would cause the program to stop due to the error. The program should prompt the user for a second set of ten numbers. When the user enters 9999 the program should terminate normally. For this project, do the following: Create a new project in your IDE named "ExceptionHandler." Write code to accept numbers as described above from the user. Your code should ensure that only numbers are accepted. Write code to add all values together and to divide larger numbers by smaller numbers in odd/even elements. Results should be displayed. Loop to get another set of ten numbers from the user until the user enters 9999 as the first number. Use exception handling to handle division by zero and out of range exceptions. In the catch blocks, be sure to display the cause of the exception to the user. Make sure your exception handler will handle any other errors as a default. Create a UML activity diagram that illustrates how your program works. Your diagram must include all the data items in your program. Use good comments to describe how the program works.
Explanation / Answer
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ExceptionHandling {
public static final int EXIT = 9999;
/**
* indefinitely takes user input until user enters
* termination value
*/
public void start() {
while (true) {
try {
doUserOperation();
} catch(Exception e) {
e.printStackTrace();
System.out.println("Exception encountered while processing, err message = "+e.getMessage());
}
}
}
/**
*
* reads ten integer values from user, performs sum
* and divisions
*
*/
public void doUserOperation() {
Scanner sc = new Scanner(System.in);
int array[] = new int[10];
/*
* Read user input values
* */
System.out.print("Enter ten values : ");
for (int i=0; i<10; i++) {
try {
array[i] = sc.nextInt();
if (i==0 && array[i]==EXIT) {
System.exit(0);
}
} catch (InputMismatchException e) {
System.out.println("Error : The entered elements are not integer values");
e.printStackTrace();
sc.close();
return;
} catch (NoSuchElementException e) {
System.out.println("Error : user input has been exhausted");
e.printStackTrace();
sc.close();
return;
}
}
sc.close();
/*
* Find out the sum
*/
int sum = 0;
for (int i=0; i<10; i++) {
try {
/*
* Checking if overflow or underflow occurs when added
* */
checkFlowWhenAdded(sum, array[i]);
sum += array[i];
} catch(NumberFormatException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return;
}
}
System.out.println("Sum of the elements is : "+sum);
/*
* Finding out min and max elements from odd and even numers
*/
int maxOdd=Integer.MAX_VALUE, minOdd=Integer.MIN_VALUE;
int maxEven=Integer.MAX_VALUE, minEven=Integer.MIN_VALUE;
for (int i=0; i<10; i++) {
if (array[i]%2==0) {
if (maxEven < array[i]) {
maxEven = array[i];
}
if (minEven > array[i]) {
minEven = array[i];
}
}
else {
if (maxOdd < array[i]) {
maxOdd = array[i];
}
if (minOdd > array[i]) {
minOdd = array[i];
}
}
}
try {
/*
* performing divisions and handling div-by-zero error
*/
double resultOdd = maxOdd/minOdd;
double resultEven = maxEven/minEven;
System.out.println("Division (maxOdd/minOdd) is " + resultOdd);
System.out.println("Division (maxEven/minOdd) is " + resultEven);
} catch (ArithmeticException e) {
System.out.println("Error : Division by zero error!");
e.printStackTrace();
return;
}
}
/**
* This method checks if addtion of a and b results in either
* overflow or underflow conditions. If so, then throws NumberFormatException
* with appropriate message
*
* @param a
* @param b
*/
private void checkFlowWhenAdded(int a, int b) {
if (a > 0 && b > 0 && a > (Integer.MAX_VALUE - b)) {
throw new NumberFormatException("Erro : Addtion resulted into an overflow arithmetic, "
+ "values are [a=" + a + ", b=" + b + "]");
}
else if (a < 0 && b < 0 && a < (Integer.MIN_VALUE - b)) {
throw new NumberFormatException("Erro : Addtion resulted into an underflow arithmetic, "
+ "values are [a=" + a + ", b=" + b + "]");
}
}
public static void main(String args[]) {
new ExceptionHandling().start();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.