Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Pythagorean theorem calculator applet problem

1. ## Pythagorean theorem calculator applet problem

i am trying to make a javascript Pythagorean theorem calculator that calculates the hypotenuse of sides a and b, i have this code but im not sure how to get the 2 sides to calculate.
Code:
```// The "Components" class.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonDemo extends JFrame implements ActionListener
{
// Declare objects
JLabel label;
JLabel label2;
JButton button1, button2;
JTextField text;
JTextField text2;

public ButtonDemo ()
{
super ("ButtonDemo");

// Set up the frame
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setBounds (300, 300, 200, 350);
// Create objects
JPanel content = new JPanel();
label = new JLabel ("10",JLabel.CENTER);
label2 = new JLabel ("20",JLabel.CENTER);
text = new JTextField (10);
text2 = new JTextField (10);
button1 = new JButton ("Calculate");
button2 = new JButton ("Done");

// Show the contents
this.setContentPane (content);
this.setVisible (true);
}

public void actionPerformed (ActionEvent e)
{
if (e.getSource () == button1)
{
a = Double.parseDouble (text1.getText());
}
double a = Math.sqrt(a*a+b*b);
{
a = 0;		// just make it zero for simplicity
}
{
b= Double.parseDouble (text1.getText());
}
double b = Math.sqrt(a*a+b*b);
{
b = 0;		// just make it zero for simplicity
}
double c = Math.sqrt(a*a+b*b);
{
label.setText ("The hyp is:" + c);
}
}
else if (e.getSource () == button2)
{
label.setText ("Exit.");
System.exit(0);

}

} // main method
} // Components class```

• You should note that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

• This is syntactically wrong; you have an else belonging to nothing in here. You also have a number of undeclared variables.

PHP Code:
```     public void actionPerformed (ActionEvent e)    {        double a, b, c;        a = b = c = 0.0;        if (e.getSource () == button1)        {            try            {                a = Double.parseDouble(text.getText());                b = Double.parseDouble(text2.getText());                c = Math.sqrt(a * a + b * b);                label.setText("The hyp is: " + c);            }            catch (NumberFormatException ex)            {                JOptionPane.showMessageDialog(null,                        "One or more entries are invalid.\n" +                        "Please use only double numbers",                        "Error",                        JOptionPane.ERROR_MESSAGE);            }        }        else if (e.getSource() == button2)        {            System.exit(0);        }    }  ```

•