Back Next

Calculates the hypotenuse of a triangle

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class Triangle3 extends JApplet implements ActionListener
{
private JLabel Label1, Label2, answerLabel;
private JTextField Field1,Field2, answerField;
private double answer, side1, side2;
private DecimalFormat precisionThree;

public void init()
{
precisionThree=new DecimalFormat("0.000");
Container c = getContentPane();
Label1 = new JLabel("Enter first value");
Field1=new JTextField();
Label2 = new JLabel( "Enter second value" );
Field2 = new JTextField();
Field2.addActionListener(this);
answerLabel = new JLabel("The length of the hypotenuse is");
answerField = new JTextField();
c.setLayout(new GridLayout(0,2,50,10));
c.add(Label1);
c.add(Field1);
c.add(Label2 );
c.add(Field2);
c.add(answerLabel);
c.add(answerField); 
answerField.setEditable(false);
this.setSize(450, 100); 
}

public void actionPerformed( ActionEvent e )
{
side1=(double)Double.valueOf(Field1.getText()).doubleValue();
side2=(double)Double.valueOf(Field2.getText()).doubleValue();
answer = Math.sqrt( Math.pow(side1,2)+Math.pow( side2,2));
answerField.setText(""+precisionThree.format(answer));
}

}

Up