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

Exception Assignment 1. Create a user defind exception class called nonintresult

ID: 3830778 • Letter: E

Question

Exception Assignment

1. Create a user defind exception class called nonintresultexception which is generated when the result

of dividing two integers values produces a result with a fractional component[ i.e the result is not an integer]

nonintresultexception contains:

i. a constructor with parameter that represent the two integer vlaues

ii. generates an appropriate message , for example if the two integers are 7 and 2 . the resulting exception message would be 7 divided by 2 is not an integer

2.create the integerarraymath class's integer division method(see the below code) the method:

i.loops thru instance field array and attempts to divide each value of number array by the correponding value of denom instance field array. such as number[0]/denom[0] and number[1]/denom[1],etc

ii. if the result of the division is an integer then print out a message indicating the result of the division such as 8/4 is 2.

iii. if the result of the division is not a integer then throw and handle a nonintresult exceptoin and continue processing the result of the number array elements.

iv. The method should, using exception handling also handle ay attempt to divide by zero(arithmetic exception) the program should display an appropriate message and then continue processing the rest of the number array elements

5. Assume both arrays are the same length. Start of IntegerArrayMath:

class IntegerArrayMath

{

int[] number;

int[] denom;

// constructor to initialize the above two arrays

//NonIntResultException class code goes here

public void integerDivision()

{

// your code goes here

}

}

Number is [4,8,15] and denom is [2,0,4]

The resultant output would be:

4/2 is 2

Division by zero is undefined

result 15 divided by 4 is not an integer

}

test the above requirements with test class

Explanation / Answer

#######

class NonIntResultException extends Exception{

  

   String message;

   public NonIntResultException(int n, int d) {

       message = n+" divided by "+d+" is not an integer";

   }

  

   @Override

   public String getMessage() {

       return message;

   }

}

#########

public class IntegerArrayMath

{

   int[] number;

   int[] denom;

   // constructor to initialize the above two arrays

   //NonIntResultException class code goes here

   public void integerDivision()

   {

       // your code goes here

       int i = 0;

       while(i < number.length){

          

           try{

              

               if(number[i] %denom[i] != 0)

                   throw new NonIntResultException(number[i], denom[i]);

               System.out.println(number[0]+"/"+denom[i]+" is "+(number[i]/denom[i]));

              

           }catch (NonIntResultException e) {

               System.out.println(e.getMessage());

           }catch(ArithmeticException e){

               System.out.println("Division by zero is undefined");

           }

          

           i++;

       }

   }

  

   public static void main(String[] args) {

       IntegerArrayMath obj = new IntegerArrayMath();

       int numbers[] = {4,8,15};

       int denoms[] = {2,0,4};

      

       obj.number = numbers;

       obj.denom = denoms;

      

       obj.integerDivision();

   }

}

/*

Sample run:

4/2 is 2

Division by zero is undefined

15 divided by 4 is not an integer

*/