// DivideByZeroExceptionTester.java
// APPLET CODE="DivideByZeroExceptionTester" WIDTH=750 HEIGHT=400
// Demo with the try-catch code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DivideByZeroExceptionTester extends JApplet {
	Container c;
	JLabel prompt1, prompt2, prompt3;
	JTextField input1, input2, output;
	int n, d;
	double q;

	public void init( ) {
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		prompt1 = new JLabel( "Enter the numerator:  " );
		c.add( prompt1 );
		input1 = new JTextField( 10 );
		c.add( input1 );
		prompt2 = new JLabel( "Enter the denominator:  " );
		c.add( prompt2 );

		input2 = new JTextField( 10 );
		input2.addActionListener(
			new ActionListener( ) {
				public void actionPerformed( ActionEvent e ) {
					n = Integer.parseInt( input1.getText( ) );
					d = Integer.parseInt( input2.getText( ) );
					try {
						q = quotient ( n, d );
						output.setText( Double.toString( q ) );
					}
					catch ( DivideByZeroException ex ) {
						output.setText( ex.toString( ) );
					}
				}
			}
		);
					
		c.add( input2 );

		prompt3 = new JLabel( "Quotient: " );
		c.add( prompt3 );
		output = new JTextField( 60 );
		c.add( output );
	}

	public double quotient ( int numer, int denom ) throws DivideByZeroException {
		if ( denom == 0 ) 
			throw new DivideByZeroException( );
		return ( double ) numer / denom;
	}
}