/**============================================================== 
 *  This program is testing 5 different exceptions. The purpose
 * of this program is to help me practice and understand how to
 * handle exceptions using a try-catch block.
 *
 * @author  Mesbaul Haque
 * @version 1
 * 
//==============================================================*/
// Date:  January 17, 2007
// Section 3
// Lab 10-03

public class HandlingExceptions
{
   /** ========================================================
	 * 
	 *  main method - in general will only call other methods
	 *              - required by every java application
    *
    *  @param args   command line arguments not used in this application
    */
	public static void main(String[] args)
	{
	   // Constants and variables for ArithmeticException test
		final int ONE      = 1; // Constant value 1
		final int ZERO     = 0; // Constant value 2
		      int numer;        // Numerator
				int denom;        // Denominator
				int quotient;     // Result of division operation
		
		// Variable for IndexOutOfBoundsException test
		int[] numbers; // Array of integers
		
		// Variable for NegativeArraySizeException test
		int[] intVals; // Array of integer values
		
		// Variable for NullPointerException test
		String name; // String for holding a name
		
		// Variable for IllegalArgumentException test
		String flavor; // String for holding a flavor
		
		
		/******************************************************
		 * ArithmeticException 
		 *****************************************************/
		System.out.println("Running ArithmeticException Test\n"); // Start Test
		
		// Initializing values
		numer = ONE;
		denom = ZERO;
		
		// Dividing by 0 causes an ArithmeticException
		try
		{
		   quotient = numer / denom; // Dividing the numerator by the denominator
		}
		catch(ArithmeticException ae) // Catching the exception
		{
		   // Handling exception by printing out message
			System.out.println("A number divided by 0 is undefined"); 
		}
		System.out.println("\nFinishing ArithmeticException Test\n\n\n"); // End Test
		
		
		/******************************************************
		 * IndexOutOfBoundsException 
		 *****************************************************/
		System.out.println("Running IndexOutOfBoundsException Test\n"); // Start Test
		
		// Initializing numbers with values 1, 2, 3
		numbers = new int[] {1, 2, 3}; 
		
		// Trying to get the value in the 4th index of an array 
		// that only has 3 indices causes an ArrayIndexOutOfBoundsException
		try 
		{
		   for (int ii = 0; ii < 4; ii++)
		   {
		      System.out.println(numbers[ii]); // Printing the values in numbers.
		   }
		}
		catch(ArrayIndexOutOfBoundsException aiobe) // Catching the exception
		{
		   // Handling exception by printing out message
			System.out.println("The array only has " + numbers.length 
			   + " values in it.");
		}
		System.out.println("\nFinishing IndexOutOfBoundsException Test\n\n\n"); // End Test
		
		
		/******************************************************
		 * NegativeArraySizeException
		 *****************************************************/
		System.out.println("Running NegativeArraySizeException Test\n"); // Start Test
		
		// Setting an array size to a negative number causes a
		// NegativeArraySizeException
		try
		{
		   intVals = new int[-3]; // Initializing array size to negative number
		}
		catch(NegativeArraySizeException nase) // Catching the exception
		{
		    // Handling exception by intializing the array size to the absolute
			 // value of the negative number and printing out a message
			 intVals = new int[Math.abs(-3)];
			 System.out.println("The array size has been set to the absolute "
			    + "value of -3.");
		} 
		System.out.println("\nFinishing NegativeArraySizeException Test\n\n\n"); // End Test
		
		
		/******************************************************
		 * NullPointerException
		 *****************************************************/
		System.out.println("Running NullPointerException Test\n"); // Start Test
		
		name = null; // Initializing name
		
		// Trying to find the length of a reference variable set to null
		// causes a NullPointerException
		try
		{
		   // Trying to get the length of the null String
	      System.out.println("Length of name: " + name.length());
		}
		catch(NullPointerException npe) // Catching the exception
		{
		   // Handling exception by printing out message
			System.out.println("You cannot find the length of the null String.");
		}
		System.out.println("\nFinishing NullPointerException Test\n\n\n"); // End Test
		
		
		/******************************************************
		 * IllegalArgumentException
		 *****************************************************/
		System.out.println("Running IllegalArgumentException Test\n"); // Start Test
		
		flavor = "vanilla"; // Initializing value
		
		// When using a format string or printf and the format specifier 
		// corresponding to the argument is of an incompatible type, an 
		// IllegalArgumentException is thrown.
		try
		{
		   System.out.printf("%c is the flavor you chose.", flavor);
		}
		catch(IllegalArgumentException ifce) // Catching the exception
		{
		   // Handling exception by printing out message
			System.out.println("A format specifier corresponding to a particular "
			   + "argument in printf is of an incompatible type with the argument.");
		}
		System.out.println("\nFinishing IllegalArgumentException Test\n\n\n"); // End Test
		
		
		// Successful completion message
		System.out.println("The program successfully completed.");
	}
}