SITE MADE BY KUSHAL SHAH

Thursday 1 December 2011

practical 24

0 comments
package practicehard;

/**
 *
 * @author Manan
 */
class RangeException extends Exception {

    public RangeException() {
        super(); // Calling Constructor of Super Class
    }
    public static void RangeCheck(double t) throws RangeException {
        if (t < 0 || t > 1) {//Checking Condition for Range of Double Parameter
            throw new RangeException();
        }
    }
    @Override
    public String toString() {
        return "Value_Out_of_Range_Exception:";//Message of Exception when Thrown
    }
}

public class Practical24 {

    double Sum = 0.0d;
    static int m = 0; //static int m for knowing the element of array throwing exception

    public double add(String StringArray[]) throws NumberFormatException, RangeException {
        Double t;       
        while (m < StringArray.length) {
            t = Double.parseDouble(StringArray[m]);//Converting String values to Double Values
            //parseDouble methode is capable of throwing NumberFormatException
            RangeException.RangeCheck(t);//calling RangeCheck methode of RangeException Class
            Sum = Sum + t;
            System.out.println("Sum=" + Sum);
            m++;
        }
        return Sum;
    }

    public static void main(String args[]) {
        String PassArray1[] = {"10m.67", "0.34", "0.78", "0.24"};//Array one for NumberFormatException
        String PassArray2[] = {"9.34","-0.88", "0.00", "1.0"};//Array two for RangeException
        String PassArray3[] = {"0.78", "0.24","0.00", "1.0", "0.5"};//Array three for proper functioning
        Practical24 p24_1 = new Practical24();
        try {
            p24_1.add(PassArray1); //Calling add methode with array as argument
        } catch (NumberFormatException e) {
            System.out.println(e + " The Number at " + m + "th position.");
        } catch (RangeException e) {
            System.out.println(e + " The Number at " + m + "th position.");
        }
        try {
            p24_1.add(PassArray2);//Calling add methode with array as argument
        } catch (NumberFormatException e) {
            System.out.println(e + " The Number at " + m + "th position.");
        } catch (RangeException e) {
            System.out.println(e + " The Number at " + m + "th position.");
        }
        try {
            p24_1.add(PassArray3);
        } catch (NumberFormatException e) {//Calling add methode with array as argument
            System.out.println(e + " The Number at " + m + "th position.");
        } catch (RangeException e) {
            System.out.println(e + " The Number at " + m + "th position.");
        }finally{
            System.out.println("Thank You for using this Program");
        }
    }
}