Sunday, 12 March 2023

7. Write an application that accepts marks of three different subject from user. Marks should be between 0 to 100, if marks of any of the subject is not belong to this range, generate custom exception out of RangeException. If marks of each subjects are greater than or equal to 40 then display message “PASS” along with percentage, otherwise display message “FAIL”. Also write exception handling code to catch all the possible runtime exceptions likely to be generated in the program.

 

import java.util.*;
 
 class RangeException extends Exception
 {
     RangeException(int i)
     {
         super("RangeException : Marks " + i + " is invalid ");
     }
 }
 
 class Program7
 {
     public static void main(String args[])
     {
         int arr[] = new int[3];
         int sum=0;
         float per = 0.0f;
         
         for(int i=0; i<3; i++)
         {
             try
             {
                Scanner sc = new Scanner(System.in);
                System.out.print("enter marks subject " + (i+1) + ": ");
                arr[i] = sc.nextInt();
               
                if(arr[i] < 0 || arr[i] > 100)
                {
                    throw new RangeException(arr[i]);
                }
                else if(arr[i] >= 40)
                {
                    sum = sum+arr[i];
                    System.out.println("\npass in subject " + i+1);
                }
                else
                {
                    sum = sum+arr[i];
                    System.out.println("\nfail in subject " + i+1);
                }
             }
             catch(ArrayIndexOutOfBoundsException e)
             {
                 System.out.println(e.getMessage());
             }
             catch(NumberFormatException n)
             {
                 System.out.println(n.getMessage());
             }
             catch(ArithmeticException a)
             {
                 System.out.println(a.getMessage());
             }
             catch(Exception e)
             {
                 System.out.println(e.getMessage());
             }
         }
         
         per = (float)sum / 3;
         
         System.out.println("\nPercentage: " + per);
     }
 }


No comments:

Post a Comment

python programs

1. sum of two number