Wednesday, March 11, 2009

 /**
 Programmer: Cuming Mary Grace
 Program name: User-Friendly  Division
 Date Started:MArch 2,2009
 Date Finished: MArch 11, 2009
 Purpose of the programm:  To learn more about Exception Handling in a program
 */
 
 
 
 import java.util.InputMismatchException;
  import java.util.Scanner;
  
  public class DivisionPractice
  {

  // demonstrates throwing an exception using the Arithmetic Exception when a divide-by-zero occurs


  public static int quotient( int numerator, int denominator )
  throws ArithmeticException
  {
      return numerator / denominator; // possible division by zero

  } // end method quotient

 
  public static void main( String args[] )
  {
  Scanner scanner = new Scanner( System.in ); // scanner for input
  boolean continueLoop = true; // determines if more input is needed
   
   
   //the try block
  do  

  {  

  try // read two numbers and calculate quotient  
  {  
  System.out.print( "Please enter an integer numerator: " );
  int numerator = scanner.nextInt();  
  System.out.print( "Please enter an integer denominator: " );
  int denominator = scanner.nextInt();  
      
       int result = quotient( numerator, denominator );  
  System.out.printf( "\nResult: %d / %d = %d\n", numerator,  
  denominator, result );  
  continueLoop = false; // input successful; end looping  
  } // end try  
  catch ( InputMismatchException inputMismatchException )  
  {  
  System.err.printf( "\nException: %s\n",  
  inputMismatchException );  
  scanner.nextLine(); // discard input so user can try again  
  System.out.println(  
  "You must enter integers. Please try again.\n" );  
  } // end catch  
  catch ( ArithmeticException arithmeticException )  
  {  
  System.err.printf( "\nException: %s\n", arithmeticException );
  System.out.println(  
  "Zero is an invalid denominator. Please try again.\n" );
  } // end catch  
  } while ( continueLoop ); // end do...while  
  } // end main
  
  } // end class DivideByZeroWithExceptionHandling  

No comments:

Post a Comment