Friday, 27 November 2015

Number Check Exception


     Create a class called CalcAverage that has the following method:

public double avgFirstN(int N)


This method receives an integer as a parameter and calculates the average of first N natural numbers. If N is not a natural number, throw an exception IllegalArgumentException with an appropriate message.


package om;
public class Test {
    int c;
   
    public double avgFirstN(int N)
    {
        if(N<0)
        {
            try{
                throw new NumberFormatException();
            }
            catch(NumberFormatException nf){
                System.out.println("We Must To Enter The POsitive Number");
            }
        }
        else
        {
            int c=N*10;
       
        System.out.println("THe Average is a = "+c);
        }
       
        return N;   
    }
   
public static void main(String[] args) {
       
       
        Test t=new Test();
        t.avgFirstN(10);   
    }

}

1 comment: