Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree2Likes

Thread: Hypotenuse Calculations

  1. #1
    Nightcrawler is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Hypotenuse Calculations

    I have a programming assignment that is supposed to take the lengths of the two legs of a given right triangle and calculate the hypotenuse. My code is below, but when I try to compile, it says that my main method needs to be declared as public static void. If I make it static, however, I am told that I cannot reference non-static content within a static context. What should I do?

    Java Code:
    package winniethepooh;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class WinniethePooh extends JApplet implements ActionListener
    {
        JTextField inputA;
        JTextField inputB;
        JLabel firstmandate;
        JLabel secondmandate;
    
        @Override public void init()
        {
        inputA = new JTextField(4);
        inputB = new JTextField(4);
        inputB.addActionListener(this);
        firstmandate = new JLabel("Enter the length of leg A: ");
        secondmandate = new JLabel("Enter the length of leg B: ");
    
        Container content = getContentPane();
    
        content.setLayout(new FlowLayout());
        content.add(inputA);
        content.add(inputB);
        content.add(firstmandate);
        content.add(secondmandate);
        }
    
        @Override public void actionPerformed(java.awt.event.ActionEvent e)
        {
        double legA;
        double legB;
    
        legA = Double.parseDouble(inputA.getText());
        legB = Double.parseDouble(inputB.getText());
    
        double hypotenuse = hypotenuse(legA, legB);
            showStatus("The length of the hypotenuse is: " + hypotenuse);
        }
    
        public double hypotneuse(double legA, double legB)
        {
            double hypotenusesquared = Math.pow(legA, 2) + Math.pow(legB, 2);
                return Math.sqrt(hypotenusesquared);
        }
    }
    Last edited by Nightcrawler; 03-17-2012 at 06:34 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hypotenuse Calculations

    I don't see a main( ... ) method whatsoever and a JApplet doesn't need one.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hypotenuse Calculations

    First off, are you tasked to write an Applet or an application?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Nightcrawler is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Hypotenuse Calculations

    An application. Also, I didn't think I needed to import JApplet, I tried importing javax.swing.*, java.awt.event.*, and java.awt.* and was told that it didn't follow the specified coding rules, so I told Netbeans to organise imports and it added JApplet instead. But the project file is a Java Application.
    Last edited by Nightcrawler; 03-17-2012 at 07:19 PM.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    I have a programming assignment that is supposed to take the lengths of the two legs of a given right triangle and calculate the hypotenuse. My code is below, but when I try to compile, it says that my main method needs to be declared as public static void. If I make it static, however, I am told that I cannot reference non-static content within a static context. What should I do?

    Java Code:
    package winniethepooh;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class WinniethePooh extends JApplet implements ActionListener
    {
        JTextField inputA;
        JTextField inputB;
        JLabel firstmandate;
        JLabel secondmandate;
    
        public void main(String[] args)
        {
        inputA = new JTextField(4);
        inputB = new JTextField(4);
        inputB.addActionListener(this);
        firstmandate = new JLabel("Enter the length of leg A: ");
        secondmandate = new JLabel("Enter the length of leg B: ");
    
        Container content = getContentPane();
    
        content.setLayout(new FlowLayout());
        content.add(inputA);
        content.add(inputB);
        content.add(firstmandate);
        content.add(secondmandate);
        }
    
        @Override public void actionPerformed(java.awt.event.ActionEvent e)
        {
        double legA;
        double legB;
    
        legA = Double.parseDouble(inputA.getText());
        legB = Double.parseDouble(inputB.getText());
    
        double hypotenuse = hypotenuse(legA, legB);
            showStatus("The length of the hypotenuse is: " + hypotenuse);
        }
    
        public double hypotneuse(double legA, double legB)
        {
            double hypotenusesquared = Math.pow(legA, 2) + Math.pow(legB, 2);
                return Math.sqrt(hypotenusesquared);
        }
    }
    line 45: spelling mistake replace "hypotneuse" by "hypotenuse"

    regards
    dhililp

  6. #6
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    I have a programming assignment that is supposed to take the lengths of the two legs of a given right triangle and calculate the hypotenuse. My code is below, but when I try to compile, it says that my main method needs to be declared as public static void. If I make it static, however, I am told that I cannot reference non-static content within a static context. What should I do?

    Java Code:
    package winniethepooh;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class WinniethePooh extends JApplet implements ActionListener
    {
        JTextField inputA;
        JTextField inputB;
        JLabel firstmandate;
        JLabel secondmandate;
    
        public void main(String[] args)
        {
        inputA = new JTextField(4);
        inputB = new JTextField(4);
        inputB.addActionListener(this);
        firstmandate = new JLabel("Enter the length of leg A: ");
        secondmandate = new JLabel("Enter the length of leg B: ");
    
        Container content = getContentPane();
    
        content.setLayout(new FlowLayout());
        content.add(inputA);
        content.add(inputB);
        content.add(firstmandate);
        content.add(secondmandate);
        }
    
        @Override public void actionPerformed(java.awt.event.ActionEvent e)
        {
        double legA;
        double legB;
    
        legA = Double.parseDouble(inputA.getText());
        legB = Double.parseDouble(inputB.getText());
    
        double hypotenuse = hypotenuse(legA, legB);
            showStatus("The length of the hypotenuse is: " + hypotenuse);
        }
    
        public double hypotneuse(double legA, double legB)
        {
            double hypotenusesquared = Math.pow(legA, 2) + Math.pow(legB, 2);
                return Math.sqrt(hypotenusesquared);
        }
    }
    this would work
    Java Code:
        import java.awt.Container;
        import java.awt.FlowLayout;
        import java.awt.event.ActionListener;
        import javax.swing.JApplet;
        import javax.swing.JLabel;
        import javax.swing.JTextField;
         
        public class WinniethePooh extends JApplet implements ActionListener
        {
            JTextField inputA;
            JTextField inputB;
            JLabel firstmandate;
            JLabel secondmandate;
         
            @Override public void init()
            {
            inputA = new JTextField(4);
            inputB = new JTextField(4);
            inputB.addActionListener(this);
            firstmandate = new JLabel("Enter the length of leg A: ");
            secondmandate = new JLabel("Enter the length of leg B: ");
         
            Container content = getContentPane();
         
            content.setLayout(new FlowLayout());
            content.add(inputA);
            content.add(inputB);
            content.add(firstmandate);
            content.add(secondmandate);
            }
         
            @Override public void actionPerformed(java.awt.event.ActionEvent e)
            {
            double legA;
            double legB;
         
            legA = Double.parseDouble(inputA.getText());
            legB = Double.parseDouble(inputB.getText());
         
            double hypotenuse = hypotneuse(legA, legB);
                showStatus("The length of the hypotenuse is: " + hypotenuse);
            }
         
            public double hypotneuse(double legA, double legB)
            {
                double hypotenusesquared = Math.pow(legA, 2) + Math.pow(legB, 2);
                    return Math.sqrt(hypotenusesquared);
            }
           
            
        
            public static void main(String[] args)
            {
            	WinniethePooh a = new WinniethePooh(); 
            }
        }

  7. #7
    Nightcrawler is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Hypotenuse Calculations

    I fixed the spelling error, but just tacking that main method onto the end of the program does let it compile without errors, but it doesn't execute anything. It just says build successful total time: 0 seconds with a blank output pane.

  8. #8
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    I have a programming assignment that is supposed to take the lengths of the two legs of a given right triangle and calculate the hypotenuse. My code is below, but when I try to compile, it says that my main method needs to be declared as public static void. If I make it static, however, I am told that I cannot reference non-static content within a static context. What should I do?
    you can take the two legs as arguments
    Java Code:
    public class hypotenuse{
    public static void getHypotenuse(double a,double b)
    {
     System.out.println (Math.sqrt(Math.pow(a,2)+Math.pow(a,2)));
    }
    public static void main(String[] args)
    {
      String x=args[0];
      String y=args[1];
      hypotenuse h = new hypotenuse();
      getHypotenuse(Double.valueOf(x).doubleValue(),Double.valueOf(y).doubleValue()); //parsing. 'D' caps in Double during parsing 
     }
    }
    dhilip

  9. #9
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    I fixed the spelling error, but just tacking that main method onto the end of the program does let it compile without errors, but it doesn't execute anything. It just says build successful total time: 0 seconds with a blank output pane.
    add
    Java Code:
    WinniethePooh a = new WinniethePooh();  //object initialized to start the class extending the JApplet
    to the main

    regards,
    dhilip

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Hypotenuse Calculations

    since the app is an applet you don't need a main() and no new WinniethePooh() is needed

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    I have a programming assignment that is supposed to take the lengths of the two legs of a given right triangle and calculate the hypotenuse. My code is below, but when I try to compile, it says that my main method needs to be declared as public static void. If I make it static, however, I am told that I cannot reference non-static content within a static context. What should I do?
    this would work.
    Java Code:
        import java.awt.Container;
        import java.awt.FlowLayout;
        import java.awt.event.ActionListener;
        import javax.swing.JApplet;
        import javax.swing.JLabel;
        import javax.swing.JTextField;
         
        public class WinniethePooh extends JApplet implements ActionListener
        {
            JTextField inputA;
            JTextField inputB;
            JLabel firstmandate;
            JLabel secondmandate;
         
            @Override public void init()
            {
            inputA = new JTextField(4);
            inputB = new JTextField(4);
            inputB.addActionListener(this);
            firstmandate = new JLabel("Enter the length of leg A: ");
            secondmandate = new JLabel("Enter the length of leg B: ");
         
            Container content = getContentPane();
         
            content.setLayout(new FlowLayout());
            content.add(inputA);
            content.add(inputB);
            content.add(firstmandate);
            content.add(secondmandate);
            }
         
            @Override public void actionPerformed(java.awt.event.ActionEvent e)
            {
            double legA;
            double legB;
         
            legA = Double.parseDouble(inputA.getText());
            legB = Double.parseDouble(inputB.getText());
         
            double hypotenuse = hypotenuse(legA, legB);
                showStatus("The length of the hypotenuse is: " + hypotenuse);
            }
         
            public double hypotenuse(double legA, double legB)
            {
                double hypotenusesquared = Math.pow(legA, 2) + Math.pow(legB, 2);
                    return Math.sqrt(hypotenusesquared);
            }
           
            
        
            
        }
    //get errors still?
    regards
    dhilip

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by j2me64 View Post
    since the app is an applet you don't need a main() and no new WinniethePooh() is needed
    Don't even bother, this thread has become a story of the lame leading the blind (or vice versa).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by JosAH View Post
    Don't even bother, this thread has become a story of the lame leading the blind (or vice versa).

    kind regards,

    Jos
    my account had 1,00,000 rep points yesterday. someone hacked. that answers all the problems

    regards
    dhilip
    Last edited by noobplus; 03-17-2012 at 06:17 PM.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by noobplus View Post
    my account had 1,00,000 rep points yesterday. someone hacked. that answers all the problems
    Read this carefully: only reply if you know for sure that you know the answer and please test the code in your reply if you do reply. Otherwise this forum will become a mess before you know it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by JosAH View Post
    Read this carefully: only reply if you know for sure that you know the answer and please test the code in your reply if you do reply. Otherwise this forum will become a mess before you know it.

    kind regards,

    Jos
    except the "factorial()" mistake, all other codes of mine , I tested in eclipse, they showed no errors
    and yeah I was telling wrong about "add this to main".. won't repeat that

    thx
    dhilip

  16. #16
    Nightcrawler is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Hypotenuse Calculations

    Post #11 that says, "this would work." is the exact same code I originally posted...

    JosAH, if a JApplet doesn't need a main method, then what is wrong with my original code that won't compile because I don't have a public static void main method?

  17. #17
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    Post #11 that says, "this would work." is the exact same code I originally posted...

    JosAH, if a JApplet doesn't need a main method, then what is wrong with my original code that won't compile because I don't have a public static void main method?
    your code show errors? no way bro , not even run-time errors (except that spelling mistake)

    regards
    dhilip

  18. #18
    Nightcrawler is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Hypotenuse Calculations

    Other than the spelling error it's the exact same code I originally posted, and after fixing the spelling error it still won't compile because it claims there's no mian method.

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    JosAH, if a JApplet doesn't need a main method, then what is wrong with my original code that won't compile because I don't have a public static void main method?
    An Applet or JApplet doesn't need a main( ... ) method but you can't run it if it were an application. An application does need such a method. (J)Applets run in your browser or in an applet viewer.

    kind regards,

    Jos
    noobplus and Nightcrawler like this.
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Hypotenuse Calculations

    Quote Originally Posted by Nightcrawler View Post
    Other than the spelling error it's the exact same code I originally posted, and after fixing the spelling error it still won't compile because it claims there's no mian method.
    you need to run the codings using an applet.
    for eclipse,
    run-->run configurations--> java-applets(on the left side pane of the dialog box)-->click new button--> click run
    (by default, it will be in application running mode)

    regards
    dhilip

Page 1 of 2 12 LastLast

Similar Threads

  1. OO for calculations?
    By StateMachine in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 08:17 AM
  2. Time calculations
    By Jason in forum New To Java
    Replies: 6
    Last Post: 09-29-2011, 09:42 PM
  3. Rounding calculations
    By lynxbci in forum New To Java
    Replies: 9
    Last Post: 08-20-2011, 03:49 PM
  4. calculating the hypotenuse
    By Latanyar in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 09:20 AM
  5. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 03:36 AM

Posting Permissions

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