Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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");
    
            // Add contents
            content.add (label);
            content.add (label2);
            content.add (button1);
            content.add (button2);
            content.add (text);
            content.add (text2);
    
            // Add listeners
            button1.addActionListener (this);
            button2.addActionListener (this);
            text.addActionListener (this);
    
            // 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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    You should note that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 abc;
            
    0.0;

            if (
    e.getSource () == button1)
            {
                try
                {
                    
    Double.parseDouble(text.getText());
                    
    Double.parseDouble(text2.getText());
                    
    Math.sqrt(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);

            }
        } 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •