
/*

   	Tom DeDonno

	Generates a Divide By Zero Exception
	and gives user a second chance when Denominator is Entered
	as Zero

	Very Similar to Page 530..531
	
 */

public class DivideByZero {
  
  
        public class DivideByZeroException extends Exception {
          
          public DivideByZeroException( ) { super( "Dividing by Zero" ); }
          
          public DivideByZeroException( String message ) {
            super( message );
          }
        } //End class DivideByZeroException
	
	private int numerator, denominator;
	private double quotient; // Q = N/D Notre/Dame

	private int prompt( String comment ) {

		System.out.println( comment );
		return( SavitchIn.readLineInt() );
		}

	private void read( ) {

		numerator = prompt( "Enter Numerator" );
		denominator = prompt( "Enter Denominator" );
	}
	
	private void print( ) {
		
		quotient = numerator/(double)denominator;
		System.out.println( numerator + "/" +
		denominator + "=" + quotient );
	}
	
	private void doIt( ) {

		try {

			read( );
			if( denominator == 0 )
				throw new DivideByZeroException( );
			print( );	
		} catch( DivideByZeroException e ) {
			System.out.println( e.getMessage() );
			secondChance( );
		}
		System.out.println( "End of Program." );
	} //end do It
	
	public void secondChance( ) {

		System.out.println( "Try Again" );
		System.out.println( "Don't set denominator to Zero" );
		read( );
		if( denominator == 0 ) {
			System.out.println( "Two Tries is all you get" );
			System.out.println( "Program will end now" );
			System.exit( 0 );
		}

		print( );
	}

	public static void main( String[] args ) {

		DivideByZero oneTime = new DivideByZero();

		oneTime.doIt();
		}
}
		
		

