Results 1 to 20 of 22
Thread: Hypotenuse Calculations
 03172012, 06:06 PM #1Member
 Join Date
 Mar 2012
 Posts
 12
 Rep Power
 0
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 nonstatic 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; 03172012 at 07:34 PM.
 03172012, 06:15 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: Hypotenuse Calculations
I don't see a main( ... ) method whatsoever and a JApplet doesn't need one.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 03172012, 06:16 PM #3
Re: Hypotenuse Calculations
First off, are you tasked to write an Applet or an application?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 03172012, 06:25 PM #4Member
 Join Date
 Mar 2012
 Posts
 12
 Rep Power
 0
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; 03172012 at 08:19 PM.
 03172012, 06:33 PM #5
Re: Hypotenuse Calculations
 03172012, 06:36 PM #6
Re: Hypotenuse Calculations
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(); } }
 03172012, 06:51 PM #7Member
 Join Date
 Mar 2012
 Posts
 12
 Rep Power
 0
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.
 03172012, 06:52 PM #8
Re: Hypotenuse Calculations
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 } }
 03172012, 06:55 PM #9
Re: Hypotenuse Calculations
 03172012, 07:00 PM #10
Re: Hypotenuse Calculations
since the app is an applet you don't need a main() and no new WinniethePooh() is needed
 03172012, 07:06 PM #11
Re: Hypotenuse Calculations
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?
dhilip
 03172012, 07:07 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
 03172012, 07:14 PM #13
Re: Hypotenuse Calculations
Last edited by noobplus; 03172012 at 07:17 PM.
 03172012, 07:20 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: Hypotenuse Calculations
I have the stamina of a seal; I lie on the beach instead of running on it.
 03172012, 07:23 PM #15
Re: Hypotenuse Calculations
 03172012, 07:34 PM #16Member
 Join Date
 Mar 2012
 Posts
 12
 Rep Power
 0
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?
 03172012, 07:39 PM #17
Re: Hypotenuse Calculations
 03172012, 07:52 PM #18Member
 Join Date
 Mar 2012
 Posts
 12
 Rep Power
 0
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.
 03172012, 07:53 PM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: Hypotenuse Calculations
I have the stamina of a seal; I lie on the beach instead of running on it.
 03172012, 07:57 PM #20
Re: Hypotenuse Calculations
Similar Threads

OO for calculations?
By StateMachine in forum New To JavaReplies: 1Last Post: 12312011, 09:17 AM 
Time calculations
By Jason in forum New To JavaReplies: 6Last Post: 09292011, 09:42 PM 
Rounding calculations
By lynxbci in forum New To JavaReplies: 9Last Post: 08202011, 03:49 PM 
calculating the hypotenuse
By Latanyar in forum New To JavaReplies: 6Last Post: 10122010, 09:20 AM 
decimal calculations?
By arnab321 in forum CLDC and MIDPReplies: 5Last Post: 11192008, 04:36 AM
Bookmarks