Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Netbeans Errors occur in programs, and there are two ways to address the p

ID: 3862025 • Letter: U

Question

Using Netbeans Errors occur in programs, and there are two ways to address the problems. The first is to try to write code that checks for errors and then deals with them. For example, you can use error checking to see if a user selected a valid menu option after making a selection. Error checking tries to prevent errors from occurring. Another method is to go ahead and let errors occur but to handle them gracefully before they can affect the operation of the program. This method, called exception handling, will be the focus of this project. 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.

Explanation / Answer

Import java.io.*

class Number {

public static void main ( String args [ ] ) {

Scanner sc = new Scanner(System. in);

int arr []=new int [10];

arr [] = enterValues ();

int sum=0;

for (int i =0;i <10;i++)

sum+=arr [i];

System. out. println("Sum is"+ sum);

try {

for (int i =0; i <10; i ++){

if (arr [i]>arr [i++])

int div=arr [i+1]/ arr [i];

else

int div= arr [i]/ arr [i+1];

}

catch ( Arithmetic Exception e){

e.printStacktrace ();

}

}

Int enterValues (){

System. out. println ("enter values");

Scanner sc = new Scanner(System. in);

Int arr [] = new int [10];

for ( int k =0; k <10; k++){

arr [i]= sc.nextLine ();

if ( arr [i]== 9999)

System.exit ();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote