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

Create a main method to demonstrate the jvm throwing each of these exceptions. Y

ID: 3784763 • Letter: C

Question

Create a main method to demonstrate the jvm throwing each of these exceptions. You might have to dig around a bit to 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 not there will cause this one to be thrown.

ClassCastException

IllegalArgumentException

NullPointerException

NumberFormatException

FileNotFoundException

NegativeArraySizeException

These 2 might be tough… do them if you like a challenge.

StackOverflowError

NoClassDefFoundError   

Part 3

Demonstrate the use of multiple catch blocks as follows:

there is one line of code below that might throw an ArithmeticException, or an ArrayIndexOutOfBoundsException.

Random r = new Random();

int[] array = {10, 20};

int result = array[r.nextInt(array.length + 1)] / r.nextInt(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:

1. no exceptions,

2. ArrayIndexOutOfBoundsException

3. ArithmeticException

Part 4

Write a method called average() that accepts an array of integers called grades.

If the method does not throw an exception, it will return the average of the grades in the 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 InvalidGradeException 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 Exception 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 that the average method will work for an int array of various sizes, even 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 has invalid grades.

Explanation / Answer

part3:

import java.io.*;

import java.util.*;

class HelloWorld

{

public static float average(int grades[])

{

int i,total=0;

float avg=0;

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

{

total = total+grades[ i ];

}

avg=(float)total/10;

return (avg);

}

public static void main(String args[])

{

int [] grades={2,-1,4,3,5,1,2,4,3,5};

int i;

float avg=0;

try {

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

{

if(grades[i]>100 || grades[i]<0)

System.out.println("ERROR : InvalidGradeException");

else

avg=average(grades);

}

System.out.println("The average is: "+avg);

} catch (Exception e) {

System.out.println("InvalidGradeException :" + e);

}

}

}

    NegativeArraySizeException

part 4:

AverageExceptionTest.java

import java.util.Arrays;

public class AverageExceptionTest {

public static void main(String[] args) {

       // TODO Auto-generated method stub

       int a[] = {65, 76, 89,34, 99, 12, 67};

       int b[] = {99, 77, 56, 101, 45};

       try{

       System.out.println("******ValidGrade Test***********");

       double avg = average(a);

       System.out.println("Array Elements are "+Arrays.toString(a));

       System.out.println("Average is : "+avg);

       System.out.println("******InvalidGrade Test***********");

       avg = average(b);

       System.out.println("Array Elements are "+Arrays.toString(b));

       System.out.println("Average is : "+avg);

     

       }

       catch(Exception e){

           System.out.println("Array Elements are "+Arrays.toString(b));

           System.out.println(e);

       }

   }

   public static double average(int a[]) throws InvalidGradeException{

       int sum = 0;

       double average = 0;

       try{

           for(int i=0; i<a.length; i++){

               if(a[i] < 0 || a[i] > 100){

                   throw new InvalidGradeException("Invalid Grade at index "+i+" value is "+a[i]);

               }

               else{

                   sum = sum + a[i];

               }

           }

           average = sum / (double)a.length;

       }

       finally{

           for(int i=0; i<a.length; i++){

               if(a[i] < 60)

                   a[i] = 60;

           }

       }

       return average;

   }

}

InvalidGradeException.java

   public class InvalidGradeException extends Exception{

       String errorMsg ;

       public InvalidGradeException(String s){

           this.errorMsg = s;

       }

   public String toString(){

   return (errorMsg ) ;

   }

   }

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