Tuesday, 23 January 2018

Write a simple java application that reads marks of five subjects through command line arguments and display average. The application should generate an exception if marks are not in integer format and out of 0-100.



Program:-
class InvalidMarksEnterException extends Exception
{
            InvalidMarksEnterException()
            {
            }
}
class u61 extends Exception
{
            static int avg;
            public static void main(String a[])
            {
                        int s1,s2,s3,s4,s5;
                       
                        try
                        {
                                    s1=Integer.parseInt(a[0]);      
                                    s2=Integer.parseInt(a[1]);
                                    s3=Integer.parseInt(a[2]);      
                                    s4=Integer.parseInt(a[3]);      
                                    s5=Integer.parseInt(a[4]);      
                                   
                                    for(int i=0;i<5;i++)
                                    {
                                                if(Integer.parseInt(a[i])<=0 || Integer.parseInt(a[i])>100)
                                                {
                                                            throw new InvalidMarksEnterException();
                                                }
                                                else
                                                {
                                                            avg = (s1+s2+s3+s5+s4)/5;
                                                }
                                    }
                                                System.out.println("Average is: "+avg);
                                   
                                   
                        }catch(NumberFormatException e)
                        {
                                    System.out.println("Plaese Enter only Integer.");
                        }catch(InvalidMarksEnterException e)
                        {
                                    System.out.println("Plaese Enter valid Marks.");
                        }
            }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...