Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please i need some help with my programing assigment in Java Create a main method to demonstrate the jvm throwing each of these exceptions. You
Please i need some help with my programing assigment in Java
Create a main method to demonstrate the jvm throwing each of these exceptions. You might have to dig around a bi o find out what they are and who (what methods) throw them. Note that you are not going to create an Exception of this type and throw it, you will write a snippet of code that causes one to be thrown. ArrayIndexOutOfBoundsException = eg. Create an array and access an element that is one to be is not there will cause this one to be thrown. ClassCastException IllegalArgumentException NullPointerException NumberFormatException File Not FoundException NegativeArraySizeException These 2 might be tough... do them if you like a challenge. StackOverflowError NoClass Def ound Error Demonstrate the use of multiple catch blocks as follows: there is one line of code below that might throw an ArithmeticException, or ArrayIndexOutOfBoundsException. Random r = new Random (); int [] array = {10, 20}; int result = array {r.netInt{array. length +1}]/r.netInt(2); Explain why each of these might happen Put this code in a try block that can distinguish between which one has been thrown (2 catch blocks). Run the code until you have demonstrated all 3 possibilities: no exceptions, Array IndexoutOfBoundsExceptional ArithmeticException Write a method called average() that accepts an array of integers called grades. If the method does not throw an exccption. it will return the average of the grades array. Start by implementing that. The method will throw an exception of type InvalidGradeException.if any element of the array is greater than 100 or less than 0 You must create the InvalidGradeException class. The InyalidGradeException object should contain information about what went wrong i.e., which of the grades was invalid (its index), and what the invalid value was. The exception will be caught by the caller (the main method) and print out the information contained in the InvalidGradeException object. You are demonstrating that you can pass information from a method back to the caller, by putting it in an Except on object and throwing it. The average () method will change all the grades in the array to 60 that are less than 60 after the average is calculated. This will also be done if an exception is thrown, and the average is not calculated. This will be done in a finally block INSIDE the average method. Demonstrate at the average method will work for a int array of various sizes, given though the method has only one parameter. Demonstrate that your method will work with an array that has no invalid grades, AND with an array that valid gradesStep 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