Sunday, 12 March 2023

9. Write an application that converts between meters and feet. Its first command line argument is a number and second command line argument is either "centimeter" or "meter". If the argument equals "centimeter" displays a string reporting the equivalent number of meters. If this argument equals "meters", display a string reporting the equivalent number of centimeter. If unit is not given properly then generate custom exception Unitformatexception. If first argument is not proper format then generate numberformatexception. Generate other exception as per requirements. (1 meter=100 centimeter)

 

class Unitformatexception extends Exception
 {
     Unitformatexception(String s)
     {
         super("unit is not valid: " + s);
     }
 }
 
 class Program9
 {
     public static void main(String args[])
     {
         int no;
         String str;
         
         try
         {
             no = Integer.parseInt(args[0]);
             str = args[1];
             
             if((str.equals("centimeter")) || (str.equals("meter")))
             {
                 if(str.equals("centimeter"))
                 {
                     int m = no/100;
                     System.out.println("meter is : " + m);
                 }
                 else if(str.equals("meter"))
                 {
                     int cm = no*100;
                     System.out.println("centimeter is : " + cm);

                 }
                 else
                     throw new Unitformatexception(str);
             }
         }
         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());
             }
     }
 }
 


No comments:

Post a Comment

python programs

1. sum of two number