Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

Posted By: Ryan Bouchard     Category: Java     Views: 4235

A program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException.
Take the number from the command line. And sort the some of that numbers you are passing from the command line (For example: the numbers that u have passed is 14 15 17 18 20 then the sort 5 6 8 9 2 like 2 5 6 8 9).

Code for Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException in Java

class ExcSort
{
       publicstaticvoid sort(int arr[]) throws ArithmeticException,
       ArrayIndexOutOfBoundsException, NullPointerException
       {
              int rem=0;
              int sum_dig_arr[]={0, 0, 0, 0, 0};
              System.out.println("\n"+"The Summed Array:");
              for(int i=0; i<5; i++)
              {
                     while(arr[i]!=0)
                     {
                         rem=arr[i]%10;
                         sum_dig_arr[i]=sum_dig_arr[i]+rem;
                         arr[i]=arr[i]/10;
                     }
                     System.out.println(sum_dig_arr[i]);
              }
              System.out.println("\n"+"The Sorted Array:");
              for(int i=0; i<5; i++)
              {
                   for(int j=i+1; j<5; j++)
                   {
                        if(sum_dig_arr[j] < sum_dig_arr[i])
                        {
                              int temp=sum_dig_arr[i];
                              sum_dig_arr[i]=sum_dig_arr[j];
                              sum_dig_arr[j]=temp;
                        }
                   }
                   System.out.println(sum_dig_arr[i]);
              }
             // throw new ArithmeticException("Caught...");// throw new ArrayIndexOutOfBoundsException("Caught...");// throw new NullPointerException("Caught...");
       } 
     
       publicstaticvoid main(String args[])
       {
              int array[]=newint[5];
              for(int i=0; i<5; i++)
              {
                     array[i]=Integer.parseInt(args[i]);
              }
              try
              {
                     sort(array);
              }
              catch(ArithmeticException ae)  
              {
                      System.out.println("Caught "+ae);
              }       
              catch(ArrayIndexOutOfBoundsException ie)
              {
                      System.out.println("Caught "+ie);
              }       
              catch(NullPointerException ne)
              {
                      System.out.println("Caught "+ne);
              }       
       }    
}


/*
Output

java ExcSort 34 2 45 21 5

The Summed Array:
7
2
9
3
5

The Sorted Array:
2
3
5
7
9

*/
  
Share: 



Ryan Bouchard
Ryan Bouchard author of Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException is from Montreal, Canada.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!