java program 7


java program 7:
Create a user defined exception named “CheckArguments” to check the arguments passed through command line. If no of arguments are less than 5, throw the “CheckArgument” exception else print addition and avg. of all 5 nos.
import java.lang.Exception;
import java.util.Scanner;
import java.io.*;
class CheckArguments extends Exception
{
CheckArguments(String message)
{super(message);
}
}
class arguments
{
public static void main(String args[])
{

int a,count=0,sum=0,avg;
System.out.println("enter numbers") ;
for(int i=0;i<args.length;i++)
{ a=Integer.parseInt(args[i]);
count=count+1;
sum=sum+a;
}
avg=sum/i;
try
{
if(count<5)
{
throw new CheckArguments("Arguments are greater than 5");
}
 else
{
    System.out.println(sum);
    System.out.println(avg);
}
 }
catch (CheckArguments e)
{System.out.println("Caught the exception");
}
finally
{
System.out.println("i am always here");
}


}
}

Comments

Popular posts from this blog

REFLECTION OF A TRIANGLE (COMPUTER GRAPHICS)