// ExerciseD4_15.java
// ExplicitCasting.java
// Computes hypotenuse given two integer sides converted to doubles
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExplicitCasting extends JApplet implements ActionListener {
	// GUI Components
	Container c;
	JLabel labelS1, labelS2, labelH;
	JTextField inputS1, inputS2, outputH;

	public void init() {
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		labelS1 = new JLabel( "Enter first side as an integer:  " );
		inputS1 = new JTextField( 10 );
		labelS2 = new JLabel( "Enter second side as an integer:  " );
		inputS2 = new JTextField( 10 );
		inputS2.addActionListener( this );  // will trigger event only from this component
		labelH = new JLabel( "Hypotenuse is:  " );
		outputH = new JTextField( 10 );
		outputH.setEditable( false );  // prevents tampering with the textfield
		c.add( labelS1 );
		c.add( inputS1 );
		c.add( labelS2 );
		c.add( inputS2 );
		c.add( labelH );
		c.add( outputH );
	}

	public void actionPerformed ( ActionEvent e ) {
		// convert text to integers
		int side1 = Integer.parseInt( inputS1.getText( ) );
		int side2 = Integer.parseInt( inputS2.getText( ) );

		// call calcHypot casting the integer sides into doubles
		double hypot = calcHypot( ( double ) side1, ( double ) side2 );
		outputH.setText( Double.toString( hypot ) );
	}

	double calcHypot( double s1, double s2 ) {
		return Math.sqrt( s1 * s1 + s2 * s2 );
	}
}