2014-11-10 HW01 update

posted Nov 10, 2014, 11:39 AM by Samuel Konstantinovich   [ updated Nov 10, 2014, 11:39 AM ]
Modify your SuperArray so that it throws IndexOutOfBoundsException instead of printing the error and returning null.

Example:

public static double foo(int numerator, int denominator){
   if( denominator == 0){
      throw new ArithmeticException();
   }
   return (float)numerator/denominator;
}

public static void main(String[]args){
   for(int a = 0; a < 1000; a++){
     try{
          System.out.println( foo( a, (int)(Math.random()*1000) ) );//print a random fraction
     } 
     catch(ArithmeticException e){
          System.out.println("Math isn't for you!"); //print an error when the foo function fails.
     } 

   }
     
}



Comments