Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Pythagorean theorem java applet problem

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pythagorean theorem java applet problem

    im not sure whats wrong with my code but i need help fixing it. I need to get the hypotenuse of sides a and b and i am having problems in the 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,139
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Pythagorean theorem java applet problem

    i am having problems
    Please explain.
    If you are getting errors, copy the message and paste it here.
    If the results are wrong, post the results and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Four color theorem help
    By fpritt24 in forum Algorithms & Recursion
    Replies: 15
    Last Post: February 19th, 2014, 05:04 PM
  2. A problem with Java 7 and a certain applet
    By Northpower98 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 25th, 2013, 02:11 AM
  3. Tell me the Problem in that java Applet
    By maz3r in forum Java Applets
    Replies: 5
    Last Post: August 20th, 2012, 11:23 AM
  4. Problem with Java Classes, in an applet
    By SashaThompson in forum Object Oriented Programming
    Replies: 12
    Last Post: October 2nd, 2011, 07:15 AM
  5. Java applet programming problem
    By danielparry in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2011, 10:31 AM

Tags for this Thread